public async Task<ListGood> AddGoodToListAsync(int userId, ListGood listGood) { if (string.IsNullOrEmpty(listGood.Title) || listGood.Amount == 0) { throw new DomainException("Incorrect input"); } var list = await _listManager.GetListAsync(userId, listGood.ListId); if (list == null) { throw new ForbiddenException(Constants.Messages.Access.UpdateList); } var existingListGood = await _listGoodRepository.GetAsync(listGood.ListId, listGood.Title); if(existingListGood != null) { if (!existingListGood.Completed) { existingListGood.Amount = listGood.Amount; existingListGood.Unit = listGood.Unit; await _listGoodRepository.UpdateAsync(existingListGood); } return existingListGood; } var existingGood = await _goodRepository.GetAsync(userId, listGood.Title); if (existingGood == null) { var good = new Good { Title = listGood.Title, Unit = listGood.Unit, CreatedDate = _dateTimeService.UtcNow, UserId = userId }; await _goodRepository.AddAsync(good); } listGood.CreatedDate = _dateTimeService.UtcNow; return await _listGoodRepository.AddAsync(listGood); }
public async Task RemoveGoodFromListAsync(int userId, ListGood good) { var list = await _listManager.GetListAsync(userId, good.ListId); if (list == null) { throw new ForbiddenException(Constants.Messages.Access.UpdateList); } var listGood = await _listGoodRepository.GetByIdAsync(good.Id); if(listGood != null) { await _listGoodRepository.RemoveAsync(good); } }
public async Task UnCompleteGoodInListAsync(int userId, ListGood good) { var list = await _listManager.GetListAsync(userId, good.ListId); if (list == null) { throw new ForbiddenException(Constants.Messages.Access.UpdateList); } var listGood = await _listGoodRepository.GetByIdAsync(good.Id); if (listGood != null && listGood.Completed) { listGood.Completed = false; listGood.CompletedDate = null; listGood.CompletedUserId = null; await _listGoodRepository.UpdateAsync(listGood); } }