Example #1
0
        public async Task <IActionResult> Put(int id, [FromBody] ProductForMallViewModel productForMallVm)
        {
            if (productForMallVm == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var dbItem = await _productForMallRepository.GetSingleAsync(id);

            if (dbItem == null)
            {
                return(NotFound());
            }
            Mapper.Map(productForMallVm, dbItem);
            dbItem.SetModification(UserName);
            _productForMallRepository.Update(dbItem);
            if (!await UnitOfWork.SaveAsync())
            {
                return(StatusCode(500, "保存时出错"));
            }
            var vm = Mapper.Map <ProductForMallViewModel>(dbItem);

            return(Ok(vm));
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] ProductForMallViewModel productForMallVm)
        {
            if (productForMallVm == null)
            {
                return(BadRequest());
            }

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

            var newItem = Mapper.Map <ProductForMall>(productForMallVm);

            newItem.SetCreation(UserName);
            _productForMallRepository.Add(newItem);
            if (!await UnitOfWork.SaveAsync())
            {
                return(StatusCode(500, "保存时出错"));
            }

            var vm = Mapper.Map <ProductForMallViewModel>(newItem);

            return(CreatedAtRoute("GetProductForMall", new { id = vm.Id }, vm));
        }