Пример #1
0
        public async Task <IHttpActionResult> PutPageRating(int id, PageRating pageRating)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != pageRating.Id)
            {
                return(BadRequest());
            }

            db.Entry(pageRating).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PageRatingExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #2
0
        public async Task <int> PostPageRatingAsync(ModelActionSockets modelActionSockets)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                PageRating pageRating = await ctx.PageRatings.FirstOrDefaultAsync(pr =>
                                                                                  pr.UserId == modelActionSockets.SenderId && pr.PageId == modelActionSockets.ReceiverId);

                if (pageRating == null)
                {
                    await ctx.PageRatings.AddAsync(new PageRating
                    {
                        UserId = modelActionSockets.SenderId,
                        PageId = modelActionSockets.ReceiverId,
                        Rating = int.Parse(modelActionSockets.Value.ToString())
                    });
                }
                else
                {
                    pageRating.Rating = int.Parse(modelActionSockets.Value.ToString());
                    ctx.PageRatings.Update(pageRating);
                }

                await ctx.SaveChangesAsync();

                return(0);
            }
        }
Пример #3
0
        public async Task <IHttpActionResult> GetPageRating(int id)
        {
            PageRating pageRating = await db.PageRatings.FindAsync(id);

            if (pageRating == null)
            {
                return(NotFound());
            }

            return(Ok(pageRating));
        }
Пример #4
0
        public async Task <IHttpActionResult> PostPageRating(PageRating pageRating)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.PageRatings.Add(pageRating);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = pageRating.Id }, pageRating));
        }
Пример #5
0
        public async Task <IHttpActionResult> DeletePageRating(int id)
        {
            PageRating pageRating = await db.PageRatings.FindAsync(id);

            if (pageRating == null)
            {
                return(NotFound());
            }

            db.PageRatings.Remove(pageRating);
            await db.SaveChangesAsync();

            return(Ok(pageRating));
        }
Пример #6
0
        public async Task <UserSocketsModel> GetUserByIdAsync(int senderId, int receiverId)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                bool[] bools      = new bool[8];
                bool   areFriends = UsersAreFriends(senderId, receiverId);
                bools[0] = areFriends;
                UserAction userAction = await ctx.UserActions.FirstOrDefaultAsync
                                            (ua => ua.SenderId == senderId && ua.ReceiverId == receiverId);

                if (userAction != null)
                {
                    bools[1] = userAction.IsFriendRequest;

                    bools[2] = userAction.IsReport;
                    bools[5] = userAction.IsFollowPage;
                }
                else
                {
                    bools[1] = false;
                    bools[2] = false;
                    bools[5] = false;
                }

                userAction = await ctx.UserActions.FirstOrDefaultAsync(ua =>
                                                                       ua.SenderId == receiverId && ua.ReceiverId == senderId);

                if (userAction == null)
                {
                    bools[3] = false;
                    bools[4] = false;
                    bools[6] = false;
                }
                else
                {
                    bools[3] = userAction.IsShareTrainings;
                    bools[4] = userAction.IsShareDiets;
                    bools[6] = userAction.IsFriendRequest;
                }

                bools[7] = false;
                if (IsUserPage(receiverId))
                {
                    PageRating pageRating = await ctx.PageRatings.FirstOrDefaultAsync(pr => pr.UserId == senderId &&
                                                                                      pr.PageId == receiverId);

                    bools[7] = pageRating != null;
                }

                if (IsUserPage(senderId))
                {
                    bools[5] = userAction?.IsFollowPage ?? false;
                }

                User user = await ctx.Users.Where(u => u.Id == receiverId)
                            .Include(u => u.Address).FirstAsync();

                int relevantFriendsNumber;
                if (!IsUserPage(senderId) && !IsUserPage(receiverId))
                {
                    relevantFriendsNumber = areFriends
                        ? GetTotalNumberOfFriendsForUser(receiverId)
                        : GetNumberOfCommonFriends(senderId, receiverId);
                }
                else if (IsUserPage(senderId) && !IsUserPage(receiverId))
                {
                    relevantFriendsNumber = UserFollowsGym(receiverId, senderId)
                        ? GetTotalNumberOfFriendsForUser(receiverId) : 0;
                }
                else
                {
                    relevantFriendsNumber = GetTotalNumberOfFollowersForGym(receiverId);
                }

                UserSocketsModel userSocketsModel = new UserSocketsModel
                {
                    Id                    = user.Id,
                    Email                 = user.Email,
                    Name                  = user.Name,
                    Description           = user.Description,
                    City                  = user.City,
                    Score                 = user.Score,
                    Address               = user.Address,
                    UserStatus            = bools,
                    RelevantFriendsNumber = relevantFriendsNumber,
                    Rating                = 0
                };

                if (!IsUserPage(receiverId))
                {
                    return(userSocketsModel);
                }

                double averageRating;
                if (ctx.PageRatings.Any(pr => pr.PageId == receiverId))
                {
                    averageRating = ctx.PageRatings.Where(pr => pr.PageId == receiverId).Average(pr => pr.Rating);
                }
                else
                {
                    averageRating = 0;
                }
                Console.WriteLine("Page average rating is " + averageRating);
                userSocketsModel.Rating = Convert.ToInt32(averageRating);
                return(userSocketsModel);
            }
        }