Exemple #1
0
        public IHttpActionResult RateUser(int userId, float grade)
        {
            var usr = authenticator.AuthorizeGetUser(Request, db);

            if (usr == null)
            {
                return(Unauthorized());
            }

            var userToRate = db.ApplicationUsers.Find(userId);

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

            var rating = db.Ratings.Find(userToRate.RatingID);

            if (rating == null)
            {
                rating = db.Ratings.Add(new Rating());
                db.SaveChanges();
                userToRate.RatingID = rating.Id;
                db.SaveChanges();
            }

            float rate;

            if (rating.NumberOfRates < 3)
            {
                rate = 6f;
            }

            else
            {
                rate = rating.Sum / rating.NumberOfRates;
            }

            rating.NumberOfRates += 1;
            rating.Sum           += grade;

            db.SaveChanges();
            rate = rating.Sum / rating.NumberOfRates;
            return(Json(rate));
        }
        public IHttpActionResult Post(EventViewModel evt)
        {
            var usr = authenticator.AuthorizeGetUser(Request, db);

            if (usr == null)
            {
                return(Unauthorized());
            }

            var evtDb = db.Events.Add(
                new Event(evt));

            evtDb.CreatorId = usr.Id;
            db.SaveChanges();
            evt.Id = evtDb.Id;
            return(Ok(evt));
        }