public HttpResponseMessage Validate(int establishmentId, int establishmentNameId, EstablishmentNameApiModel model) { //System.Threading.Thread.Sleep(2000); // test api latency model.OwnerId = establishmentId; model.Id = establishmentNameId; ValidationResult validationResult; string propertyName; if (model.OwnerId < 1 || model.Id < 1) { var command = new CreateEstablishmentName(User); Mapper.Map(model, command); validationResult = _createValidator.Validate(command); propertyName = command.PropertyName(y => y.Text); } else { var command = new UpdateEstablishmentName(User); Mapper.Map(model, command); validationResult = _updateValidator.Validate(command); propertyName = command.PropertyName(y => y.Text); } Func<ValidationFailure, bool> forText = x => x.PropertyName == propertyName; if (validationResult.Errors.Any(forText)) return Request.CreateResponse(HttpStatusCode.BadRequest, validationResult.Errors.First(forText).ErrorMessage, "text/plain"); return Request.CreateResponse(HttpStatusCode.OK); }
public HttpResponseMessage Post(int establishmentId, EstablishmentNameApiModel model) { //System.Threading.Thread.Sleep(2000); // test api latency if (!FindResources(establishmentId)) throw new HttpResponseException(HttpStatusCode.NotFound); model.OwnerId = establishmentId; var command = new CreateEstablishmentName(User); Mapper.Map(model, command); try { _createHandler.Handle(command); } catch (ValidationException ex) { var badRequest = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message, "text/plain"); return badRequest; } //var response = Request.CreateResponse(HttpStatusCode.Created, Get(establishmentId, command.Id)); var response = Request.CreateResponse(HttpStatusCode.Created, string.Format("Establishment name '{0}' was successfully created.", model.Text)); var url = Url.Link(null, new { controller = "EstablishmentNames", action = "Get", establishmentId, establishmentNameId = command.Id, }); Debug.Assert(url != null); response.Headers.Location = new Uri(url); return response; }
public HttpResponseMessage Put(int establishmentId, int establishmentNameId, EstablishmentNameApiModel model) { //System.Threading.Thread.Sleep(2000); // test api latency if (!FindResources(establishmentId, establishmentNameId)) throw new HttpResponseException(HttpStatusCode.NotFound); model.OwnerId = establishmentId; model.Id = establishmentNameId; var command = new UpdateEstablishmentName(User); Mapper.Map(model, command); try { _updateHandler.Handle(command); } catch (ValidationException ex) { var badRequest = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message, "text/plain"); return badRequest; } var response = Request.CreateResponse(HttpStatusCode.OK, "Establishment name was successfully updated."); return response; }