Exemplo n.º 1
0
        public ActionResult Update(int id, EditItemModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var item = moneyRepository.GetItem(model.Id);

            if (item == null)
            {
                return(NotFound());
            }

            if (item.UserId != CurrentUserId)
            {
                return(Unauthorized());
            }

            moneyRepository.UpdateItem(new Item
            {
                Id         = model.Id,
                Name       = model.Description,
                CategoryId = model.CategoryId,
                Cost       = model.Cost.Value,
                Type       = model.Type
            });

            return(Ok());
        }
Exemplo n.º 2
0
        public ActionResult GetItemById(int id)
        {
            var item = moneyRepository.GetItem(id);

            if (item == null)
            {
                return(NotFound());
            }

            if (item.UserId != CurrentUserId)
            {
                return(BadRequest());
            }

            var vm = new EditItemModel
            {
                Id          = item.Id,
                CategoryId  = item.CategoryId,
                Cost        = item.Cost,
                Description = item.Name,
                Type        = item.Type
            };

            return(Ok(vm));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> EditItem(long itemId, [FromBody] EditItemModel model)
        {
            try
            {
                model.UserId = CurrentUserId;

                model.Id = itemId;

                var result = await itemLib.EditItemAsync(model);

                return(CustomResult(result));
            }
            catch (System.Exception exp)
            {
                return(CustomError(exp));
            }
        }
Exemplo n.º 4
0
        public async Task <ItemModel> EditItemAsync(EditItemModel model)
        {
            var entity = await itemRepo.FirstAsync(x => x.Id == model.Id);

            if (entity == null)
            {
                throw new Exception("Item Not Exist");
            }

            if (entity.UserId != model.UserId)
            {
                throw new Exception("User Doesn't Owner Of Item");
            }

            entity.Name = model.Name;

            itemRepo.Update(entity);

            await unitOfWork.CommitAsync();

            return(ConvertItemToItemModel(entity));
        }
Exemplo n.º 5
0
 private void InitData()
 {
     Model = new EditItemModel();
 }