// POST: api/Producer public IHttpActionResult Post(ProducerRequestModel producer) { if (producer == null) { return(this.BadRequest("Invalid producer!")); } this.producerData.Add(new Producer { Name = producer.Name }); this.producerData.SaveChanges(); return(this.Ok("Priducer added")); }
public IHttpActionResult Post([FromBody] ProducerRequestModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var entity = new Producer { Name = model.Name }; this.context.AddEntity(entity); this.context.Save(); return(this.Created(this.Url.ToString(), entity)); }
public IHttpActionResult Post(ProducerRequestModel request) { if (request == null) { return this.BadRequest(GlobalMessages.EntityMustNotBeNullMessage); } var producer = new Producer { FirstName = request.FirstName, LastName = request.LastName, Age = request.Age }; Country defaultCountry = this.data.CountriesRepository.All().FirstOrDefault(); producer.Country = defaultCountry; this.data.ProducersRepository.Add(producer); int result = this.data.SaveChanges(); return this.Ok($"{GlobalMessages.EntitySuccessfullyAddedMessage} - {result}"); }
public IHttpActionResult Update(string id, ProducerRequestModel request) { if (id == null) { return this.BadRequest(GlobalMessages.IdMustNotBeNullMessage); } if (request == null) { return this.BadRequest(GlobalMessages.EntityMustNotBeNullMessage); } Producer entity = this.data.ProducersRepository.FindById(id); if (entity == null) { return this.BadRequest(GlobalMessages.EntityDoesNotExist); } // Map the request model to the db model if (request.FirstName != default(string)) { entity.FirstName = request.FirstName; } if (request.LastName != default(string)) { entity.LastName = request.LastName; } if (request.Age != default(short)) { entity.Age = request.Age; } this.data.ProducersRepository.Update(entity); int result = this.data.SaveChanges(); return this.Ok($"{GlobalMessages.EntitySuccessfullyUpdatedMessage} - {result}"); }
public async Task <OperationDataResult <ProducersModel> > Index(ProducerRequestModel requestModel) { return(await _producerService.Index(requestModel)); }
public async Task <OperationDataResult <ProducersModel> > Index(ProducerRequestModel pnRequestModel) { try { var producersModel = new ProducersModel(); var producersQuery = _dbContext.Producers.AsQueryable(); if (!pnRequestModel.NameFilter.IsNullOrEmpty() && pnRequestModel.NameFilter != "") { producersQuery = producersQuery.Where(x => x.Name.Contains(pnRequestModel.NameFilter) || x.Description.Contains(pnRequestModel.NameFilter)); } if (!string.IsNullOrEmpty(pnRequestModel.Sort)) { if (pnRequestModel.IsSortDsc) { producersQuery = producersQuery .CustomOrderByDescending(pnRequestModel.Sort); } else { producersQuery = producersQuery .CustomOrderBy(pnRequestModel.Sort); } } else { producersQuery = _dbContext.Producers .OrderBy(x => x.Id); } producersQuery = producersQuery .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Skip(pnRequestModel.Offset) .Take(pnRequestModel.PageSize); var producers = await producersQuery.Select(x => new ProducerModel { Id = x.Id, Name = x.Name, Description = x.Description, ForeignId = x.ForeignId, Address = x.Address, City = x.City, ZipCode = x.ZipCode, Phone = x.Phone, ContactPerson = x.ContactPerson }).ToListAsync(); producersModel.Total = _dbContext.Producers.Count(x => x.WorkflowState != Constants.WorkflowStates.Removed); producersModel.ProducerList = producers; return(new OperationDataResult <ProducersModel>(true, producersModel)); } catch (Exception e) { Trace.TraceError(e.Message); _coreHelper.LogException(e.Message); return(new OperationDataResult <ProducersModel>(false, _trashInspectionLocalizationService.GetString("ErrorObtainingProducers"))); } }