public BaseResponse UpdateItem(string userId, ItemDto itemDto) { try { userId.ThrowIfNullOrEmpty(); itemDto.ThrowIfNull(); var itemToUpdate = _itemsRepository.All() .FirstOrDefault(x => x.Id == itemDto.Id && x.UserId == userId); itemToUpdate.ThrowIfNullWithShowMessage($"Current user does not have item with id {itemDto.Id}"); itemToUpdate.Name = itemDto.Name; itemToUpdate.ReplenishmentPeriod = itemDto.ReplenishmentPeriod; itemToUpdate.NextReplenishmentDate = itemDto.NextReplenishmentDate; itemToUpdate.UserId = userId; _itemsRepository.Update(itemToUpdate); _itemsRepository.SaveChanges(); var response = ResponseBuilder.Success(); return(response); } catch (ShowErrorMessageException ex) { var response = ResponseBuilder.Failure <ItemDto>(ex.Message); return(response); } catch (Exception ex) { var response = ResponseBuilder.Failure("An error occurred while updating item"); return(response); } }
public BaseResponse AddItem(string userId, ItemDto itemDto) { try { userId.ThrowIfNullOrEmpty(); itemDto.ThrowIfNull(); var itemEntity = _mapper.Map <ItemDto, Item>(itemDto); itemEntity.UserId = userId; _itemsRepository.Add(itemEntity); _itemsRepository.SaveChanges(); var response = ResponseBuilder.Success(); return(response); } catch (Exception ex) { var response = ResponseBuilder.Failure("An error occurred while creating item"); return(response); } }