public async Task <UserGroup> AddUserToGroup(string userGroupId, ShoppingUserModel user)
        {
            var group = await GetAsync(userGroupId);

            var existingUser = await _userRepository.GetUserAsync(user);

            if (existingUser == null)
            {
                throw new ItemNotFoundException($"No user with provided user data found");
            }

            if (group.Members.Any(m => m.Id == existingUser.Id))
            {
                throw new ItemAlreadyExistsException($"User with '{existingUser.UserName} already exists in group: '{group.Name}'");
            }

            _context.UserGroupMembers.Add(new UserGroupMembers
            {
                MemberId    = existingUser.Id,
                UserGroupId = userGroupId
            });

            await _context.SaveChangesAsync();

            return(group);
        }
Esempio n. 2
0
        public async Task <ShoppingListItem> AddOrUpdateItemAsync(string listId, ShoppingListItem item)
        {
            var list = await GetAsync(listId);

            item.ShoppingListId = listId;
            item.ProductItem    = await GetProductAsync(item.ProductItemId);

            var itemsOfList = await GetItemsOfList(listId);

            var existing = itemsOfList.FirstOrDefault(i => i.ProductItemId == item.ProductItemId);

            if (existing == null)
            {
                _context.ShoppingListItems.Add(item);
            }
            else
            {
                existing.Done   = item.Done;
                existing.Amount = item.Amount;
            }

            await _context.SaveChangesAsync();

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

            item.Category = await GetCategoryAsync(item.CategoryId);

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

            return(item);
        }
        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 <bool> DeleteByIdAsync(string id)
        {
            var existing = await GetAsync(id);

            _context.Categories.Remove(existing);

            bool result = false;

            try
            {
                await _context.SaveChangesAsync();

                result = true;
            }
            catch
            {
                result = false;
            }

            return(result);
        }
        public async Task <bool> DeleteByIdAsync(string id)
        {
            var existing = await GetAsync(id);

            if (existing == null)
            {
                throw new ItemNotFoundException(typeof(Store), id);
            }
            _context.Stores.Remove(existing);
            bool result = false;

            try
            {
                await _context.SaveChangesAsync();

                result = true;
            }
            catch
            {
                result = false;
            }
            return(result);
        }
Esempio n. 7
0
        public async Task <StoreProductCategory> CreateAsync(StoreProductCategory storeProductCat)
        {
            if (AssignmentExists(storeProductCat))
            {
                throw new ItemAlreadyExistsException(typeof(StoreProductCategory), storeProductCat.StoreProductCategoryId);
            }
            _context.StoreProductCategories.Add(storeProductCat);
            await _context.SaveChangesAsync();

            return(storeProductCat);
        }