public async Task <IActionResult> UpdateProductCapacity(int id, [FromBody] ProductCapacitySaveResource saveResource) { if (!_auth.IsAppAdmin(User)) { return(NoContent()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var productCapacityFromRepo = await _repo.GetProductCapacity(id); if (productCapacityFromRepo == null) { return(BadRequest($"Product Capacity {id} could not be found.")); } var filter = new MdaProductCapacityQuery() { Name = saveResource.Name, ProductModelId = saveResource.ProductModelId, Active = 2 }; var productCapacityFromRepoExisting = await _repo.GetProductCapacities(filter); if (productCapacityFromRepoExisting.Any()) { var existingProductCapacity = productCapacityFromRepoExisting.FirstOrDefault(); if (existingProductCapacity.Id != id) { return(BadRequest($"Product Capacity {saveResource.Name} already exists for the specified model.")); } else { if (existingProductCapacity.Name.ToLower() == saveResource.Name.ToLower() && existingProductCapacity.ProductModelId == saveResource.ProductModelId) { if (existingProductCapacity.Active == Convert.ToByte(saveResource.Active == true ? 1 : 0)) { return(BadRequest("Nothing has changed.")); } } } } _mapper.Map <ProductCapacitySaveResource, MdaProductCapacity>(saveResource, productCapacityFromRepo); productCapacityFromRepo.ModifiedBy = User.Identity.Name; productCapacityFromRepo.ModifiedDate = DateTime.Now; if (await _repo.SaveAll()) { return(NoContent()); } return(BadRequest("Failed to update Product Capacity")); }
public async Task <PagedList <MdaProductCapacity> > GetProductCapacities(MdaProductCapacityQuery filter) { var query = _context.MdaProductCapacity .Include(m => m.ProductModel).ThenInclude(m => m.ProductManufacturer) .Include(m => m.ProductModel).ThenInclude(t => t.ProductType) .Include(p => p.MdaProduct) // .IgnoreQueryFilters() .AsQueryable(); // if (filter.PageSize == 0) // filter.PageSize = 10; if (!string.IsNullOrEmpty(filter.Name)) { // query = query.Where(pc => pc.Name == filter.Name); query = query.Where(pc => pc.Name.StartsWith(filter.Name)); } if (filter.ProductTypeId.HasValue) { query = query.Where(pc => pc.ProductModel.ProductType.Id == filter.ProductTypeId); } if (filter.ProductModelId.HasValue) { query = query.Where(pc => pc.ProductModelId == filter.ProductModelId); } if (filter.ProductManufacturerId.HasValue) { query = query.Where(pc => pc.ProductModel.ProductManufacturer.Id == filter.ProductManufacturerId); } if (filter.Active == 0) { query = query.Where(d => d.Active == 0); } if (filter.Active == 1) { query = query.Where(d => d.Active == 1); } var columnsMap = new Dictionary <string, Expression <Func <MdaProductCapacity, object> > > { ["name"] = a => a.Name }; query = query.ApplyOrdering(filter, columnsMap); // query = query.ApplyPaging(filter); // return await query.ToListAsync(); return(await PagedList <MdaProductCapacity> .CreateAsync(query, filter.Page, filter.PageSize)); }
public async Task <IActionResult> AddProductCapacity([FromBody] ProductCapacitySaveResource saveResource) { if (!_auth.IsAppAdmin(User)) { return(NoContent()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var filter = new MdaProductCapacityQuery() { Name = saveResource.Name, ProductModelId = saveResource.ProductModelId }; var productCapacityFromRepo = await _repo.GetProductCapacities(filter); if (productCapacityFromRepo.Any()) { return(BadRequest($"Product Capacity {saveResource.Name} already exists for the specified model.")); } var productCapacity = _mapper.Map <MdaProductCapacity>(saveResource); productCapacity.CreatedBy = User.Identity.Name; _repo.Add(productCapacity); if (await _repo.SaveAll()) { return(Ok(productCapacity)); } return(BadRequest("Failed to add product capacity.")); }