コード例 #1
0
ファイル: StoreService.cs プロジェクト: dev-ips/Lifvs-Server
        public bool RateStore(StoreRateModel model)
        {
            if (model.Rating <= 0)
            {
                throw _exception.ThrowException(System.Net.HttpStatusCode.BadRequest, "", "Ogiltigt värde för butik betyg.");
            }

            var store = _storeRepository.GetStoreDetailById(model.StoreId);

            if (store == null)
            {
                throw _exception.ThrowException(System.Net.HttpStatusCode.BadRequest, "", "Butiken finns inte.");
            }

            var storeRating = new StoreRatings
            {
                StoreId = model.StoreId,
                UserId  = model.UserId,
                Rating  = model.Rating
            };

            _storeRepository.AddStoreRatings(storeRating);
            var averageRatings = _storeRepository.GetStoreAverageRatings(model.StoreId);

            store.Rating = averageRatings;
            return(_storeRepository.UpdateStoreRating(store));
        }
コード例 #2
0
        public bool AddStoreRatings(StoreRatings model)
        {
            var sqlQuery = AddStoreRatingsQuery();

            _db.Execute(sqlQuery, new
            {
                @storeId = model.StoreId,
                @userId  = model.UserId,
                @rating  = model.Rating
            });
            return(true);
        }
コード例 #3
0
        public IHttpActionResult SubmitStoreReview(ReviewBindingModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                using (DunkeyContext ctx = new DunkeyContext())
                {
                    if (ctx.StoreRatings.Any(x => x.User_Id == model.User_Id && x.Store_Id == model.Store_Id))
                    {
                        return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                        {
                            Message = "Forbidden",
                            StatusCode = (int)HttpStatusCode.Forbidden,
                            Result = new Error {
                                ErrorMessage = "User already reviews this store."
                            }
                        }));
                    }
                    else
                    {
                        var storeRating = new StoreRatings {
                            User_Id = model.User_Id, Rating = model.Rating, Store_Id = model.Store_Id, Feedback = model.Feedback
                        };
                        ctx.StoreRatings.Add(storeRating);
                        ctx.SaveChanges();
                        var latestStoreRating = ctx.StoreRatings.Include(z => z.User).FirstOrDefault(x => x.Id == storeRating.Id);
                        CustomResponse <StoreRatings> response = new CustomResponse <StoreRatings>
                        {
                            Message    = Global.SuccessMessage,
                            StatusCode = (int)HttpStatusCode.OK,
                            Result     = storeRating
                        };

                        return(Ok(response));
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(DunkeyDelivery.Utility.LogError(ex)));
            }
        }