예제 #1
0
        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);
        }
예제 #2
0
        public ApiFamily Accept(int id)
        {
            var dataFamily = context.AcceptInvitation(id);

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

            return(family);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }