private static void MapComment(DefectForm form, Defect defect) { if (string.IsNullOrEmpty(form.Comment)) return; var comment = new Comment { CommentDate = DateTime.Now, DefectId = form.Id, CommentText = form.Comment, UserId = form.UserId }; defect.Comments.Add(comment); }
// POST api/Defect public HttpResponseMessage PostDefect(DefectForm form) { if (ModelState.IsValid) { var defect = Mapper.Map<Defect>(form); _defectService.Insert(defect); var response = Request.CreateResponse(HttpStatusCode.Created, form); //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = defect.Id })); return response; } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); }
// PUT api/Defect/5 public HttpResponseMessage PutDefect(int id, DefectForm form) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != form.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } try { var defect = Mapper.Map<Defect>(form); _defectService.Update(defect); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }