public static UserApiGetResponse FromUser(User user, IEnumerable <Invitation> invitations = null) { var response = new UserApiGetResponse() { CurrentUser = user.Id }; response.Families = user.Families?.Select(f => ApiFamily.FromFamily(f)).ToArray() ?? new ApiFamily[] { }; response.Users = user.Families ?.SelectMany(f => f.Members) .Concat(new [] { user }) .Distinct() .Select(u => ApiUser.FromUser(u)) .ToArray() ?? new ApiUser[] { }; response.Invitations = invitations != null ? invitations.Select(i => ApiInvitation.FromInvitation(i)).ToArray() : new ApiInvitation[] { }; response.Lists = user.Families ?.SelectMany(f => f.Lists ?? new ShoppingList[] { }) .Distinct() .Select(l => ApiList.FromList(l)) .ToArray() ?? new ApiList[] { }; return(response); }
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 ApiList Get(int id) { var existingList = context.Lists .Include(list => list.Family) .Include(list => list.Items) .ThenInclude(li => li.Product) .SingleOrDefault(l => l.Id == id); if (existingList == null) { throw new ForagerApiException(ForagerApiExceptionCode.ListNotFound); } var list = ApiList.FromList(existingList); return(list); }
public ApiList Post(int id, [FromBody] string name) { if (string.IsNullOrWhiteSpace(name)) { throw new ForagerApiException(ForagerApiExceptionCode.InvalidNameProvided); } var existingList = context.Lists.SingleOrDefault(f => f.Id == id); if (existingList == null) { throw new ForagerApiException(ForagerApiExceptionCode.ListNotFound); } existingList.Name = name.Trim(); context.SaveChanges(); var list = ApiList.FromList(existingList); return(list); }