// POST api/alerts public HttpResponseMessage Post(AlertDTO alertDTO) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } var alert = alertDTO.ToEntity(); _unitOfWork.Alerts.Add(alert); _unitOfWork.Commit(); alertDTO.Id = alert.Id; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, alertDTO); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = alertDTO.Id })); return response; }
// DELETE api/alerts/5 public HttpResponseMessage Delete(int id) { var alert = _unitOfWork.Alerts.GetById(id); if (alert == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } var alertDTO = new AlertDTO(alert); _unitOfWork.Alerts.Delete(alert); try { _unitOfWork.Commit(); } catch { return Request.CreateResponse(HttpStatusCode.InternalServerError); } return Request.CreateResponse(HttpStatusCode.OK, alertDTO); }
// PUT api/alerts/5 public HttpResponseMessage Put(int id, AlertDTO alertDTO) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != alertDTO.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } var alert = alertDTO.ToEntity(); _unitOfWork.Alerts.Update(alert); try { _unitOfWork.Commit(); } catch { return Request.CreateResponse(HttpStatusCode.InternalServerError); } return Request.CreateResponse(HttpStatusCode.OK); }