示例#1
0
        public async Task <IActionResult> UpdateProductModel(int id, [FromBody] ProductModelSaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var productModelFromRepo = await _repo.GetProductModel(id);

            if (productModelFromRepo == null)
            {
                return(BadRequest($"Product Model {id} could not be found."));
            }

            // var filter = _mapper.Map<ProductModelSaveResource,MdaProductModelQuery>(saveResource);
            var filter = new MdaProductModelQuery()
            {
                ProductManufacturerId = saveResource.ProductManufacturerId,
                ProductTypeId         = saveResource.ProductTypeId,
                Name = saveResource.Name
            };

            var productFromRepoExisting = await _repo.GetProductModels(filter);

            if (productFromRepoExisting.Any())
            {
                var existingProductModel = productFromRepoExisting.FirstOrDefault();
                if (existingProductModel.Id != id)
                {
                    return(BadRequest($"Product Model {saveResource.Name} already exists for specified type and manufacturer."));
                }
                else
                {
                    if (existingProductModel.Name == saveResource.Name &&
                        existingProductModel.ProductManufacturerId == saveResource.ProductManufacturerId &&
                        existingProductModel.ProductTypeId == saveResource.ProductTypeId)
                    {
                        return(BadRequest("Nothing was changed."));
                    }
                }
            }

            _mapper.Map <ProductModelSaveResource, MdaProductModel>(saveResource, productModelFromRepo);
            productModelFromRepo.ModifiedBy   = User.Identity.Name;
            productModelFromRepo.ModifiedDate = DateTime.Now;

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update Product Model."));
        }
        public async Task <PagedList <MdaProductModel> > GetProductModels(MdaProductModelQuery filter)
        {
            var query = _context.MdaProductModel
                        .Include(m => m.ProductManufacturer)
                        .Include(t => t.ProductType)
                        .Include(p => p.MdaProduct)
                        .AsQueryable();

            // if (filter.PageSize == 0)
            //     filter.PageSize = 10;

            if (!string.IsNullOrEmpty(filter.Name))
            {
                query = query.Where(pm => pm.Name.Contains(filter.Name));
            }

            if (filter.ProductManufacturerId.HasValue)
            {
                query = query.Where(pm => pm.ProductManufacturerId == filter.ProductManufacturerId);
            }

            if (filter.ProductTypeId.HasValue)
            {
                query = query.Where(pm => pm.ProductTypeId == filter.ProductTypeId);
            }

            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 <MdaProductModel, object> > >
            {
            };

            query = query.ApplyOrdering(filter, columnsMap);
            // query = query.ApplyPaging(filter);
            // return await query.ToListAsync();
            return(await PagedList <MdaProductModel> .CreateAsync(query, filter.Page, filter.PageSize));
        }
示例#3
0
        public async Task <IActionResult> AddProductModel([FromBody] ProductModelSaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var filter = new MdaProductModelQuery()
            {
                ProductManufacturerId = saveResource.ProductManufacturerId,
                ProductTypeId         = saveResource.ProductTypeId,
                Name = saveResource.Name
            };

            var productModelFromRepo = await _repo.GetProductModels(filter);

            if (productModelFromRepo.Any())
            {
                return(BadRequest($"Product Model {saveResource.Name} already exists for specified type and manufacturer."));
            }

            var productModel = _mapper.Map <MdaProductModel>(saveResource);

            productModel.CreatedBy = User.Identity.Name;

            _repo.Add(productModel);

            if (await _repo.SaveAll())
            {
                return(Ok(productModel));
            }

            return(BadRequest("Failed to add product model"));
        }