public async Task<IHttpActionResult> PostAnnouncement(CreateAnnouncementModel cam) { if(!ModelState.IsValid) { return BadRequest(ModelState); } var announcement = await _announcementManager.CreateAnnouncement(cam); if(announcement == null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Could not match class to existing records")); } return CreatedAtRoute("DefaultApi", new { id = announcement.Id }, new AnnouncementViewModel(announcement)); }
/// <summary> /// Creates a new announcement from the given model /// </summary> /// <param name="cam"></param> /// <returns></returns> public async Task<Announcement> CreateAnnouncement(CreateAnnouncementModel cam) { var @class = await _db.Classes.FindAsync(cam.ClassId); if(@class == null) { return null; } Announcement announcement = new Announcement { Title = cam.Title, CreatedOn = DateTime.UtcNow, Description = cam.Description, Class = @class }; _db.Announcements.Add(announcement); await _db.SaveChangesAsync(); return announcement; }