示例#1
0
        public HttpResult GetCoachReview([FromQuery] int id)
        {
            CoachReview r = db.CoachReview.Find(id);

            return(r == null
                ? new HttpResult(false, new CoachReview(), "No Review")
                : new HttpResult(true, r, ""));
        }
示例#2
0
        public async Task <HttpResult> AddEditCoachReview([FromBody] CoachReview review)
        {
            try
            {
                if (!Functions.UserLoggedIn(Request, out User user) && !Functions.AdminLoggedIn(Request, out _))
                {
                    throw new Exception("Not logged in!");
                }

                DateTime now = DateTime.Now;

                if (review.Id > 0)
                {
                    CoachReview updating = db.CoachReview.Find(review.Id);
                    Functions.CheckNull(updating);

                    updating.ModifiedDate = now;
                    updating.Title        = review.Title;
                    updating.MainReview   = review.MainReview;
                    updating.CoachId      = review.CoachId;
                    updating.GoodPoints   = review.GoodPoints;
                    updating.BadPoints    = review.BadPoints;
                    updating.Rating       = review.Rating;

                    db.Entry(updating).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

                    await db.SaveChangesAsync();
                }
                else
                {
                    review.CreationDate = now;
                    review.ModifiedDate = now;
                    review.ReviewerId   = user.Id;

                    db.CoachReview.Add(review);

                    await db.SaveChangesAsync();
                }

                //updating gym rating.
                User coach = await db.User.FindAsync(review.CoachId);

                coach.AverageRating = db.CoachReview.Where(x => x.CoachId == coach.Id).Average(c => c.Rating);

                db.Entry(coach).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                await db.SaveChangesAsync();

                return(new HttpResult(true, review, ""));
            }
            catch (Exception e)
            {
                return(new HttpResult(false, null, Functions.ErrorMessage(e)));
            }
        }
示例#3
0
        public HttpResult GetMyCoachReview([FromQuery] int coachID)
        {
            if (!Functions.UserLoggedIn(Request, out User user) && !Functions.AdminLoggedIn(Request, out _))
            {
                return(new HttpResult(false, null, ""));
            }

            CoachReview r = db.CoachReview.FirstOrDefault(x => x.CoachId == coachID && x.ReviewerId == user.Id);

            return(r == null
                ? new HttpResult(false, new GymReview(), "No Review")
                : new HttpResult(true, r, ""));
        }
示例#4
0
        public async Task <HttpResult> DeleteCoachReview([FromRoute] int postID)
        {
            try
            {
                //    if (!Functions.AdminLoggedIn(Request, out _))
                //        throw new Exception("Not logged in!");

                CoachReview deleting = await db.CoachReview.FindAsync(postID);

                if (deleting == null)
                {
                    throw new Exception("Post not found!");
                }

                bool adminLoggedIn = Functions.AdminLoggedIn(Request, out _);
                bool userLoggedIn  = Functions.UserLoggedIn(Request, out User editor);

                if ((!adminLoggedIn && !userLoggedIn) || //no user logged in
                    (userLoggedIn && editor.Id != deleting.ReviewerId))       //or user logged in but does not own the review
                {
                    throw new Exception("Not logged in!");
                }

                //update coach rating
                User coach = await db.User.FindAsync(deleting.CoachId);

                IQueryable <CoachReview> remainingReviews = db.CoachReview.Where(x => x.CoachId == coach.Id && x.Id != deleting.Id);
                if (remainingReviews.Count() > 0)
                {
                    coach.AverageRating = remainingReviews.Average(c => c.Rating);
                }
                else
                {
                    coach.AverageRating = 0d;
                }
                db.Entry(coach).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

                db.CoachReview.Remove(deleting);

                await db.SaveChangesAsync();

                return(new HttpResult(true, null, ""));
            }
            catch (Exception e)
            {
                return(new HttpResult(false, null, Functions.ErrorMessage(e)));
            }
        }