Example #1
0
        public async Task <PagedList <MdaProductType> > GetProductTypes(MdaProductTypeQuery filter)
        {
            var query = _context.MdaProductType
                        .Include(pm => pm.MdaProductModel)
                        .AsQueryable();

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

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

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

            query = query.ApplyOrdering(filter, columnsMap);
            // query = query.ApplyPaging(filter);
            // return await query.ToListAsync();
            return(await PagedList <MdaProductType> .CreateAsync(query, filter.Page, filter.PageSize));
        }
        public async Task <IActionResult> UpdateProductType(int id, [FromBody] ProductTypeSaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

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

            /* Pre-existence Test */
            var filter = new MdaProductTypeQuery()
            {
                Name   = saveResource.Name,
                Active = Convert.ToByte(saveResource.Active == true ? 1 : 0)
            };
            var productTypeFromRepoExisting = await _repo.GetProductTypes(filter);

            if (productTypeFromRepoExisting.Any())
            {
                return(BadRequest($"Product Type {saveResource.Name} already exists."));
            }

            var productTypeFromRepo = await _repo.GetProductType(id);

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

            _mapper.Map <ProductTypeSaveResource, MdaProductType>(saveResource, productTypeFromRepo);
            productTypeFromRepo.ModifiedBy   = User.Identity.Name;
            productTypeFromRepo.ModifiedDate = DateTime.Now;

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

            return(BadRequest("Failed to update Product Type"));
        }
        public async Task <IActionResult> AddProductType([FromBody] ProductTypeSaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

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

            /* Pre-existence Test */
            var filter = new MdaProductTypeQuery()
            {
                Name = saveResource.Name
            };
            var productTypeFromRepo = await _repo.GetProductTypes(filter);

            if (productTypeFromRepo.Any())
            {
                return(BadRequest($"Product Type {saveResource.Name} already exists."));
            }

            var productType = _mapper.Map <MdaProductType>(saveResource);

            productType.CreatedBy = User.Identity.Name;

            _repo.Add(productType);

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

            return(BadRequest("Failed to add product type."));
        }