public async Task <ActionResult <UserGroupShoppingListResult> > DeleteAssignment(string groupId, string listId)
        {
            var result     = new UserGroupShoppingListResult();
            var assignment = new UserGroupShoppingList()
            {
                UserGroupId    = groupId,
                ShoppingListId = listId
            };

            try
            {
                bool deleteResult = await _userGroupShoppingLists.DeleteAsync(assignment);

                if (!deleteResult)
                {
                    result.IsSuccessful = false;
                    result.ErrorMessages.Add($"Could not delete assignment Group:'{groupId}'<-->List:'{listId}'");
                    return(UnprocessableEntity(result));
                }
                result.IsSuccessful = true;
            }
            catch (ItemNotFoundException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);
                return(NotFound(result));
            }
            catch (PersistencyException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);
                return(NotFound(result));
            }
            return(Ok(result));
        }
        public async Task <bool> DeleteAsync(UserGroupShoppingList assignment)
        {
            var existing = (await GetAllAsync())
                           .FirstOrDefault(a => a.ShoppingListId == assignment.ShoppingListId && a.UserGroupId == assignment.UserGroupId);

            bool result = false;

            if (existing != null)
            {
                _context.UserGroupShoppingLists.Remove(existing);
                try
                {
                    await _context.SaveChangesAsync();

                    result = true;
                }
                catch
                {
                    result = false;
                }
            }
            else
            {
                _logger.LogWarning($"No assignment found between list '{assignment.ShoppingListId}' and group '{assignment.UserGroupId}'");
            }

            return(result);
        }
        public bool ItemCanBeUpdated(UserGroupShoppingList item)
        {
            var assignments         = _context.UserGroupShoppingLists.ToList();
            var itemsWithoutCurrent = assignments.Where(a => a.Id != item.Id).ToList();

            return(!(itemsWithoutCurrent.Any(a => a.ShoppingListId == item.ShoppingListId && a.UserGroupId == item.UserGroupId)));
        }
        public async Task <UserGroupShoppingList> UpdateAsync(string id, UserGroupShoppingList item)
        {
            if (!ItemCanBeUpdated(item))
            {
                throw new ItemAlreadyExistsException(typeof(UserGroupShoppingList), id);
            }
            var existing = await GetAsync(id);

            existing.ShoppingListId = item.ShoppingListId;
            existing.UserGroupId    = item.UserGroupId;

            await _context.SaveChangesAsync();

            return(existing);
        }
        public async Task <UserGroupShoppingList> CreateAsync(UserGroupShoppingList item)
        {
            if (ItemAlreadyExists(item))
            {
                throw new ItemAlreadyExistsException(typeof(UserGroupShoppingList), item.Id);
            }

            item.ShoppingList = await GetShoppingListAsync(item.ShoppingListId);

            item.UserGroup = await GetUserGroupAsync(item.UserGroupId);

            _context.UserGroupShoppingLists.Add(item);
            await _context.SaveChangesAsync();

            return(item);
        }
        public async Task <ActionResult <UserGroupShoppingListResult> > CreateAssignment([FromBody] UserGroupShoppingList assignment)
        {
            var result = new UserGroupShoppingListResult();

            try
            {
                result.IsSuccessful = true;
                var created = await _userGroupShoppingLists.CreateAsync(assignment);

                result.ResultData.Add(created);
            }
            catch (ItemAlreadyExistsException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);
                return(Conflict(result));
            }
            catch (PersistencyException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);
                return(Conflict(result));
            }
            return(Ok(result));
        }
        public bool ItemAlreadyExists(UserGroupShoppingList item)
        {
            var assignments = _context.UserGroupShoppingLists.ToList();

            return(assignments.Any(a => a.Id == item.Id || (a.ShoppingListId == item.ShoppingListId && a.UserGroupId == item.UserGroupId)));
        }