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 ApiFamily Accept(int id) { var dataFamily = context.AcceptInvitation(id); context.SaveChanges(); var family = ApiFamily.FromFamily(dataFamily); return(family); }
public ApiFamily Get(int id) { var existingFamily = context.Families.Include(f => f.CreatedBy).SingleOrDefault(f => f.Id == id); if (existingFamily == null) { throw new ForagerApiException(ForagerApiExceptionCode.FamilyNotFound); } var family = ApiFamily.FromFamily(existingFamily); return(family); }
public ApiFamily Post(int id, [FromBody] string name) { if (string.IsNullOrWhiteSpace(name)) { throw new ForagerApiException(ForagerApiExceptionCode.InvalidNameProvided); } var existingFamily = context.Families.Include(f => f.CreatedBy).SingleOrDefault(f => f.Id == id); if (existingFamily == null) { throw new ForagerApiException(ForagerApiExceptionCode.FamilyNotFound); } existingFamily.Name = name.Trim(); context.SaveChanges(); var family = ApiFamily.FromFamily(existingFamily); return(family); }
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); }