public async Task <ActionResult <ShoppingListResult> > CreateList(ShoppingList shoppingList)
        {
            var user = await _users.GetUserAsync();

            shoppingList.OwnerId = user.Id;

            var result = new ShoppingListResult();

            try
            {
                result.IsSuccessful = true;
                var item = await _lists.CreateAsync(shoppingList);

                result.IsSuccessful = true;
                result.ResultData.Add(item);
            }
            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 async Task <ActionResult <ShoppingListResult> > GetList(string id)
        {
            var result = new ShoppingListResult();

            try
            {
                if (!(await IsUserAuthorizedToAccessList(id)))
                {
                    result.IsSuccessful = true;
                    result.ErrorMessages.Add("Not authorized");
                    return(Unauthorized(result));
                }

                var list = await _lists.GetAsync(id);

                result.IsSuccessful = true;
                result.ResultData.Add(list);
            }
            catch (ItemNotFoundException e)
            {
                result.IsSuccessful = true;
                result.ErrorMessages.Add(e.Message);
                return(NotFound(result));
            }

            return(Ok(result));
        }
Пример #3
0
        public ShoppingListResult CreateShoppingList(AuthIdentity identity, ShoppingList list)
        {
            using (var session = GetSession())
            {
                var ret = new ShoppingListResult();

                using (var transaction = session.BeginTransaction())
                {
                    var dbList = new Models.ShoppingLists();
                    dbList.Title  = list.Title.Trim();
                    dbList.UserId = identity.UserId;
                    session.Save(dbList);

                    if (list.Any()) // Create ShoppingListItems
                    {
                        list.ToList().ForEach(i =>
                        {
                            var dbItem          = ShoppingListItems.FromShoppingListItem(i);
                            dbItem.ShoppingList = dbList;
                            dbItem.UserId       = dbList.UserId;
                            session.Save(dbItem);
                        });
                    }

                    transaction.Commit();

                    ret.NewShoppingListId = dbList.ShoppingListId;
                }

                ret.List = list;
                return(ret);
            }
        }
        public async Task <ActionResult <ShoppingListResult> > GetLists()
        {
            var user = await _users.GetUserAsync();

            var result = new ShoppingListResult();

            result.IsSuccessful = true;
            if (await _users.IsUserAdminAsync())
            {
                result.ResultData = await _lists.GetAllAsync();
            }
            else
            {
                result.ResultData = await _lists.GetAllOfUserAsync(user.Id);
            }
            return(Ok(result));
        }
        public async Task <ActionResult <ShoppingListResult> > GetShoppingListsOfUserGroup(string id)
        {
            var result = new ShoppingListResult();

            var user = await _users.GetUserAsync();

            bool isAdmin = await _users.IsUserAdminAsync();

            bool isInGroup = await _userGroups.UserIsInGroupAsync(id, user.Id);

            if (!(isInGroup || isAdmin))
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add("Not authorized");
                return(Unauthorized(result));
            }

            result.IsSuccessful = true;
            result.ResultData   = await _userGroupShoppingLists.GetShoppingListsOfUserGroupAsync(id);

            return(Ok(result));
        }
        public async Task <ActionResult <ShoppingListResult> > UpdateList(string id, ShoppingList list)
        {
            var result = new ShoppingListResult();

            if (id != list.Id)
            {
                result.IsSuccessful = true;
                result.ErrorMessages.Add("Id does not match");
                return(BadRequest(result));
            }
            try
            {
                if (!(await IsUserAuthorizedToAccessList(list.Id)))
                {
                    result.IsSuccessful = false;
                    result.ErrorMessages.Add("Not authorized");
                    return(Unauthorized(result));
                }
                var updatedList = await _lists.UpdateAsync(id, list);

                result.ResultData.Add(updatedList);
            }
            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(Conflict(result));
            }

            return(Ok(result));
        }
        public async Task <ActionResult <ShoppingListResult> > DeleteList(string id)
        {
            var result = new ShoppingListResult();

            try
            {
                if (!(await IsUserAuthorizedToAccessList(id)))
                {
                    result.IsSuccessful = false;
                    result.ErrorMessages.Add("Not authorized");
                    return(Unauthorized(result));
                }

                var deleteResult = await _lists.DeleteByIdAsync(id);

                if (!deleteResult)
                {
                    result.IsSuccessful = false;
                    result.ErrorMessages.Add($"Could not delete list '{id}'");
                    return(UnprocessableEntity(result));
                }
            }
            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(Conflict(result));
            }
            return(Ok(result));
        }
Пример #8
0
        public ShoppingListResult CreateShoppingList(AuthIdentity identity, ShoppingList list)
        {
            using (var session = this.GetSession())
            {
                var ret = new ShoppingListResult();

                using (var transaction = session.BeginTransaction())
                {
                    var shoppingList = new ShoppingLists();
                    shoppingList.Title = list.Title.Trim();
                    shoppingList.UserId = identity.UserId;
                    session.Save(shoppingList);

                    if (list.Any())
                    {
                        list.ToList().ForEach(i =>
                        {
                            var dbItem = ShoppingListItems.FromShoppingListItem(i);
                            dbItem.ShoppingList = shoppingList;
                            dbItem.UserId = shoppingList.UserId;
                            session.Save(dbItem);
                        });
                    }

                    transaction.Commit();

                    ret.NewShoppingListId = shoppingList.ShoppingListId;
                }

                ret.List = list;
                return ret;
            }
        }