public HttpResponseMessage Put(int id, LocationApiModel locationApiModel) { if (!ModelState.IsValid) { return(Request.CreateResponseForInvalidModelState()); } using (var repos = DataRepos.Instance) { if (!id.IsLocationOwner(RequestContext.GetUserId(), repos.Locations)) { return(Request.CreateResponseForNotOwner()); } Location location = null; try { location = locationApiModel.CreateLocationFromModel(RequestContext.GetUserId(), repos.Locations, User.Identity); } catch (ArgumentException aEx) { return(Request.CreateResponseForUnprocessableEntity(aEx.Message != null ? aEx.Message : null)); } if (location.Name.Length < 1) { return(Request.CreateResponseForUnprocessableEntity("Location must have a valid name")); } else if (location.Name.Length > _MAX_LOCATION_NAME_LENGTH) { location.Name = location.Name.Substring(0, _MAX_LOCATION_NAME_LENGTH); } if (location.Desc.Length > _MAX_LOCATION_DESCRIPTION_LENGTH) { location.Desc = location.Desc.Substring(0, _MAX_LOCATION_DESCRIPTION_LENGTH); } var result = repos.Locations.Update(id, location, false); if (result) { //event EventService.Instance.Enqueue(new LocationUpdated() { Location = locationApiModel.BuildDTO(id, location.Type), }); return(Request.CreateResponse(HttpStatusCode.NoContent)); } return(Request.CreateResponse(HttpStatusCode.NotFound)); } }
public HttpResponseMessage Post(LocationApiModel locationApiModel) { if (!ModelState.IsValid) { return(Request.CreateResponseForInvalidModelState()); } using (var repos = DataRepos.Instance) { Location location = null; try { location = locationApiModel.CreateLocationFromModel(RequestContext.GetUserId(), repos.Locations, User.Identity); } catch (ArgumentException aEx) { return(Request.CreateResponseForUnprocessableEntity(aEx.Message != null ? aEx.Message : null)); } if (location.Name.Length < 1) { return(Request.CreateResponseForUnprocessableEntity("Location must have a valid name")); } else if (location.Name.Length > _MAX_LOCATION_NAME_LENGTH) { location.Name = location.Name.Substring(0, _MAX_LOCATION_NAME_LENGTH); } if (location.Desc.Length > _MAX_LOCATION_DESCRIPTION_LENGTH) { location.Desc = location.Desc.Substring(0, _MAX_LOCATION_DESCRIPTION_LENGTH); } int id = repos.Locations.Insert(location); //event EventService.Instance.Enqueue(new LocationCreated() { Location = locationApiModel.BuildDTO(id, location.Type), }); return(id != -1 ? Request.CreateResponse(HttpStatusCode.OK, id) : Request.CreateResponse(HttpStatusCode.InternalServerError)); } }