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 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 ApiProduct[] PostAll(int p = 0, int s = 0, [FromBody] int[] ignores = null) { var currentUserEmail = userInformation.GetUserEmail(); var currentUser = context.GetUserByEmail(currentUserEmail); var familiesToInclude = currentUser.Families.Select(f => f.Id).Concat(new[] { ForagerContext.UniversalFamilyId }).ToArray(); var numberToTake = p == 0 ? context.Products.Count() : p; var products = context.Families .Include(p => p.FamilyProducts) .Where(f => familiesToInclude.Contains(f.Id)) .SelectMany(f => f.FamilyProducts) .Select(fp => fp.Product) .Where(p => ignores == null || !ignores.Contains(p.Id)) .OrderBy(p => p.Id) .Distinct() .OrderBy(p => p.Name) .Skip(s) .Take(numberToTake) .Select(p => ApiProduct.FromProduct(p)) .ToArray(); return(products); }
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); }