public ApiList Put([FromBody] ListApiPutRequest listData)
        {
            if (string.IsNullOrWhiteSpace(listData.name))
            {
                throw new ForagerApiException(ForagerApiExceptionCode.InvalidNameProvided);
            }

            var currentUserEmail = userInformation.GetUserEmail();
            var currentUser      = context.GetUserByEmail(currentUserEmail);
            var currentFamily    = currentUser.Families.SingleOrDefault(f => f.Id == listData.familyId);

            if (currentFamily == null)
            {
                throw new ForagerApiException(ForagerApiExceptionCode.FamilyNotFound);
            }

            var dataList = new ShoppingList()
            {
                Name = listData.name.Trim(), Family = currentFamily
            };

            context.Lists.Add(dataList);
            context.SaveChanges();
            var list = ApiList.FromList(dataList);

            return(list);
        }
        public ApiProduct Put([FromBody] ProductApiPutRequest productData)
        {
            if (string.IsNullOrWhiteSpace(productData.Name))
            {
                throw new ForagerApiException(ForagerApiExceptionCode.InvalidNameProvided);
            }

            var currentUserEmail = userInformation.GetUserEmail();
            var currentUser      = context.GetUserByEmail(currentUserEmail);
            var dataProduct      = new Product()
            {
                Name        = productData.Name.Trim(),
                Description = productData.Description?.Trim(),
                Units       = productData.Units?.Trim(),
                CreatedBy   = currentUser,
                CreatedOn   = DateTime.Now
            };

            dataProduct.FamilyProducts = currentUser.Families.Select(f => new FamilyProducts {
                Family = f, Product = dataProduct
            }).ToArray();

            context.Products.Add(dataProduct);
            context.SaveChanges();

            var product = ApiProduct.FromProduct(dataProduct);

            return(product);
        }
Exemple #3
0
        public ApiFamily Accept(int id)
        {
            var dataFamily = context.AcceptInvitation(id);

            context.SaveChanges();
            var family = ApiFamily.FromFamily(dataFamily);

            return(family);
        }
        public static ForagerContext PrepareContext()
        {
            var user1 = new User {
                Id = 1, Email = "*****@*****.**"
            };
            var user2 = new User {
                Id = 2, Email = "*****@*****.**"
            };
            var user3 = new User {
                Id = 3, Email = "*****@*****.**"
            };
            var family1 = new Family {
                Id = 1, CreatedBy = user1
            };
            var family2 = new Family {
                Id = 2, CreatedBy = user3
            };
            var uf1 = new UserFamily {
                Family = family1, FamilyId = 1, User = user1, UserId = 1
            };
            var uf2 = new UserFamily {
                Family = family2, FamilyId = 2, User = user1, UserId = 1
            };
            var uf3 = new UserFamily {
                Family = family1, FamilyId = 1, User = user2, UserId = 2
            };
            var uf4 = new UserFamily {
                Family = family1, FamilyId = 1, User = user3, UserId = 3
            };
            var uf5 = new UserFamily {
                Family = family2, FamilyId = 2, User = user3, UserId = 3
            };
            var inv1 = new Invitation {
                Id = 1, Email = "*****@*****.**", Family = family1, Source = user2
            };
            var inv2 = new Invitation {
                Id = 2, Email = "*****@*****.**", Family = family2, Source = user1
            };
            var inv3 = new Invitation {
                Id = 3, Email = "*****@*****.**", Family = family2, Source = user2, Status = InvitationStatus.Accepted
            };
            var inv4 = new Invitation {
                Id = 4, Email = "*****@*****.**", Family = family1, Source = user1
            };
            var options = new DbContextOptionsBuilder <ForagerContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
            var context = new ForagerContext(options);

            context.Users.AddRange(user1, user2, user3);
            context.Families.AddRange(family1, family2);
            context.UserFamilies.AddRange(uf1, uf2, uf3, uf4, uf5);
            context.Invitations.AddRange(inv1, inv2, inv3, inv4);
            context.SaveChanges();
            return(context);
        }
        public ApiFamily Put([FromBody] string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ForagerApiException(ForagerApiExceptionCode.InvalidNameProvided);
            }

            var currentUserEmail = userInformation.GetUserEmail();
            var currentUser      = context.GetUserByEmail(currentUserEmail);
            var dataFamily       = new Family()
            {
                Name = name.Trim(), CreatedBy = currentUser, CreatedOn = DateTime.Now
            };

            context.Families.Add(dataFamily);
            context.LinkUserToFamily(currentUser, dataFamily);
            context.SaveChanges();
            var family = ApiFamily.FromFamily(dataFamily);

            return(family);
        }
        public UserApiGetResponse Get()
        {
            var email        = userInformation.GetUserEmail();
            var existingUser = context.GetUserByEmail(email);

            if (existingUser == null)
            {
                var name    = userInformation.GetUserName();
                var picture = userInformation.GetPicture();
                existingUser = new User {
                    Name = name, Email = email, Picture = picture
                };
                context.Users.Add(existingUser);
                context.SaveChanges();
            }

            var invitations = context.GetInvitationsForUser(existingUser);

            var response = UserApiGetResponse.FromUser(existingUser, invitations);

            return(response);
        }