Example #1
0
        public async Task <JsonResult> ActionRating([FromBody] ActionRatingParameter model)
        {
            var userString = Request.Cookies["User"];

            var userModel = userString.ToObject <User>();

            if (userModel == null)
            {
                return(Json(_responseServerService.ResponseError("Please login first !!!")));
            }

            _cache.Remove($"Champion-{model.IdChampion}");

            return(Json(await _ratingService.RatingScript(model, userModel)));
        }
Example #2
0
        public async Task <ResponseServerService> RatingScript(ActionRatingParameter model, User user)
        {
            var users           = _firebaseService.GetUsers();
            var ratings         = _firebaseService.GetRatings();
            var championScripts = _firebaseService.GetChampionScript();

            await Task.WhenAll(users, ratings, championScripts);

            var listUsers           = users.Result ?? new List <User>();
            var listRatings         = ratings.Result ?? new List <ScriptRatingDetail>();
            var listChampionScripts = championScripts.Result ?? new List <ChampionScript>();

            var championScriptFind = listChampionScripts.FirstOrDefault(a => a.Id == model.IdScript);

            if (championScriptFind == null)
            {
                return(_responseServerService.ResponseError());
            }

            var userFind = listUsers.FirstOrDefault(a => a.Email == user.Email);

            if (userFind == null)
            {
                return(_responseServerService.ResponseError());
            }

            var ratingUser = listRatings.FirstOrDefault(a => a.IdUser == userFind.Id && a.IdScript == championScriptFind.Id);

            if (ratingUser == null)
            {
                ratingUser = new ScriptRatingDetail()
                {
                    CreateDate = DateTime.Now,
                    Id         = listRatings.LastOrDefault()?.Id + 1 ?? 1,
                    IdScript   = championScriptFind.Id,
                    IdUser     = userFind.Id,
                    Point      = model.Point
                };

                listRatings.Add(ratingUser);
            }
            else
            {
                ratingUser.CreateDate = DateTime.Now;
                ratingUser.IdScript   = championScriptFind.Id;
                ratingUser.IdUser     = userFind.Id;
                ratingUser.Point      = model.Point;

                var indexof = listRatings.IndexOf(ratingUser);

                listRatings[indexof] = ratingUser;
            }

            _firebaseService.PutRatings(listRatings);

            var check = UpdateRating(listRatings, model.IdScript, listChampionScripts);

            if (check)
            {
                return(_responseServerService.ResponseSuccess("Rating Success !!!"));
            }

            return(_responseServerService.ResponseError());
        }