Esempio n. 1
0
        private Models.Suplements.Suplement mapFromDb(Database.Suplement dbSuplement, UserInfo userInfo)
        {
            Models.Suplements.Suplement suplement = new Models.Suplements.Suplement();
            _mapper.Map(dbSuplement, suplement);
            suplement.SuplementTypeName = dbSuplement.SuplementType?.Type;
            suplement.CreatedAt         = dbSuplement.CreatedAt.ToString("dd-MM-yyyy");

            if (dbSuplement.SuplementsRatings != null && dbSuplement.SuplementsRatings.Count > 0)
            {
                suplement.AverageRating = dbSuplement.SuplementsRatings.Average(x => x.Rating);

                if (userInfo != null)
                {
                    SuplementsRating userSuplementRating = dbSuplement.SuplementsRatings.Where(x => x.ClientId == userInfo.Id).FirstOrDefault();
                    if (userSuplementRating != null)
                    {
                        suplement.UserRating = userSuplementRating.Rating;
                    }
                }
            }

            return(suplement);
        }
Esempio n. 2
0
        public void CreateOrUpdateSuplementRating(SuplementRatingCreate ratingCreate, UserInfo userInfo)
        {
            if (ratingCreate.Rating < 1 || ratingCreate.Rating > 5)
            {
                throw new InvalidRatingValueException("Ocjena moze biti od 1 - 5");
            }

            Suplement suplement = _context.Suplements.Find(ratingCreate.SuplementId);

            if (suplement == null)
            {
                throw new ResourceNotFoundException($"Suplement with id: {ratingCreate.SuplementId} not found.");
            }

            SuplementsRating dbSuplementRating = _context.SuplementsRatings
                                                 .Where(x => x.ClientId == userInfo.Id)
                                                 .Where(x => x.SuplementId == ratingCreate.SuplementId)
                                                 .FirstOrDefault();

            if (dbSuplementRating == null)
            {
                dbSuplementRating = new SuplementsRating();

                dbSuplementRating.ClientId    = userInfo.Id;
                dbSuplementRating.SuplementId = ratingCreate.SuplementId;
                dbSuplementRating.Rating      = ratingCreate.Rating;
                _context.SuplementsRatings.Add(dbSuplementRating);
            }
            else
            {
                dbSuplementRating.Rating = ratingCreate.Rating;
                _context.SuplementsRatings.Update(dbSuplementRating);
            }

            _context.SaveChanges();
        }