public async Task <IHttpActionResult> PutAppRating(int id, AppRating appRating)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Esempio n. 2
0
        public AppRating Update(AppRating appRatingChanges)
        {
            var appRating = dBContext.AppRatings.Attach(appRatingChanges);

            appRating.State = EntityState.Modified;
            dBContext.SaveChanges();
            return(appRatingChanges);
        }
        public async Task <IHttpActionResult> GetAppRating(int id)
        {
            AppRating appRating = await db.AppRatings.FindAsync(id);

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

            return(Ok(appRating));
        }
        public async Task <IHttpActionResult> PostAppRating(AppRating appRating)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.AppRatings.Add(appRating);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = appRating.Id }, appRating));
        }
        public async Task <IHttpActionResult> DeleteAppRating(int id)
        {
            AppRating appRating = await db.AppRatings.FindAsync(id);

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

            db.AppRatings.Remove(appRating);
            await db.SaveChangesAsync();

            return(Ok(appRating));
        }
Esempio n. 6
0
 public AppRating Create(AppRating appRating)
 {
     dBContext.AppRatings.Add(appRating);
     dBContext.SaveChanges();
     return(appRating);
 }
Esempio n. 7
0
        public async Task <IActionResult> RateApp([FromBody] AppRating appRating)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                var user = await _userManager.FindByIdAsync(appRating.UserId);

                if (user == null)
                {
                    errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                    return(BadRequest(new { errors = errorMessages }));
                }


                var allowedValues = new List <float>()
                {
                    1, 2, 3, 4, 5
                };

                if (!allowedValues.Contains(appRating.Value))
                {
                    errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                    return(BadRequest(new { errors = errorMessages }));
                }

                var       rating = _appRatingRepository.FindByUserId(user.Id);
                AppRating createdAppRating;

                if (rating == null)
                {
                    var newRating = new AppRating()
                    {
                        User         = user,
                        UserId       = user.Id,
                        Value        = appRating.Value,
                        RateDateTime = DateTime.Now
                    };

                    createdAppRating = _appRatingRepository.Create(newRating);
                }
                else
                {
                    rating.OldValue            = rating.Value;
                    rating.Value               = appRating.Value;
                    rating.RateDateTimeUpdated = DateTime.Now;

                    createdAppRating = _appRatingRepository.Update(rating);
                }

                if (createdAppRating == null)
                {
                    errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                    return(BadRequest(new { errors = errorMessages }));
                }


                var appRatings = _appRatingRepository.GetAppRatings();


                var ratings = new
                {
                    total   = appRatings.Count > 0 ? appRatings.Sum(r => r.Value) / appRatings.Count : 0,
                    ratings = appRatings
                };

                await _hubContext.Clients.All.SendAsync("SignalAppRateReceived", new { ratings });

                return(Ok(new { ratings }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }