public async Task <IActionResult> ShopRate(UserShopRatePostRequestcs model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Petición de puntuar inválida");
                }

                return(Ok(await _userService.RateShopsAsync(model, _userManager, _apiDbContext)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
Example #2
0
        public async Task <int> RateShopsAsync(UserShopRatePostRequestcs model, UserManager <ApplicationUser> userManager, ApiDbContext apiDbContext)
        {
            try
            {
                ApplicationUser user = await userManager.FindByIdAsync(model.UserId.ToString());

                if (user == null)
                {
                    throw new Exception($"Usuario con id {model.UserId} no encontrado");
                }

                Shop shop = await apiDbContext.Shops.FindAsync(model.ShopId);

                if (shop == null)
                {
                    throw new Exception($"Tienda con id {model.ShopId} no encontrada");
                }

                User_Shop_Rating userRate = await apiDbContext.Users_Shops_Ratings.FindAsync(new { model.UserId, model.ShopId });

                if (userRate == null)
                {
                    await apiDbContext.Users_Shops_Ratings.AddAsync(new User_Shop_Rating
                    {
                        Rate    = model.Rate,
                        Date    = DateTime.Now,
                        Comment = model.Comment,
                        UserId  = model.UserId,
                        ShopId  = model.ShopId
                    });
                }
                else
                {
                    userRate.Rate    = model.Rate;
                    userRate.Date    = DateTime.Now;
                    userRate.Comment = model.Comment;
                }

                await apiDbContext.SaveChangesAsync();

                return(model.ShopId);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }