Пример #1
0
        public bool Review(Review review)
        {
            if (review == null || review.UserID == 0 || review.BeerID == 0)
            {
                throw new ArgumentNullException("review");
            }

            using (var context = new BeerBoutiqueEntities())
            {
                var rev = context.Reviews.FirstOrDefault(x => x.BeerID == review.BeerID && x.UserID == review.UserID);

                if (rev == null)
                {
                    context.Reviews.Add(review);
                }
                else
                {
                    rev.AppearanceScore = review.AppearanceScore;
                    rev.AppearanceDescription = review.AppearanceDescription;
                    rev.AromaScore = review.AromaScore;
                    rev.AromaDescription = review.AromaDescription;
                    rev.TasteScore = review.TasteScore;
                    rev.TasteDescription = review.TasteDescription;
                    rev.OverallDescription = review.OverallDescription;
                }

                context.SaveChanges();
                return true;
            }
        }
Пример #2
0
        public BreweryViewModel Get(int id)
        {
            using (var context = new BeerBoutiqueEntities()) {
                var brewery = (from x in context.Breweries
                               where x.ID == id
                               select x).FirstOrDefault();

                return new BreweryViewModel(brewery);
            }
        }
Пример #3
0
        public List<BeerViewModel> GetByBrewery(int id)
        {
            using (var context = new BeerBoutiqueEntities())
            {
                var beers = (from x in context.Beers
                             where x.BreweryID == id
                             select x).ToList();

                return beers.Select(x => new BeerViewModel(x)).ToList();
            }
        }
Пример #4
0
        public IEnumerable<ReviewViewModel> GetByBeer(int beerId)
        {
            using (var context = new BeerBoutiqueEntities()) {
                var reviews = context.Reviews.Where(x => x.BeerID == beerId).Take(10);
                var rev = new List<ReviewViewModel>();
                foreach (var review in reviews) {
                    rev.Add(new ReviewViewModel(review));
                }

                return rev;
            }
        }
Пример #5
0
        public List<BeerViewModel> GetByStyle(int id, int take = 50, int skip = 0)
        {
            using (var context = new BeerBoutiqueEntities())
            {
                var beers = (from x in context.Beers
                             where x.StyleID == id
                             select x).ToList();

                var ret = beers.Select(x => new BeerViewModel(x)).Take(take).Skip(skip).ToList();

                if (take == 0)
                    return ret;

                return ret.Take(take).ToList();
            }
        }
Пример #6
0
        public BeerViewModel Get(int id)
        {
            using (var context = new BeerBoutiqueEntities())
            {
                var beer = (from x in context.Beers
                            where x.ID == id
                            select x).FirstOrDefault();

                if (beer != null) {
                    var b = new BeerViewModel(beer);
                    b.SimilarBeers = GetByStyle(beer.StyleID, 3);
                    return b;
                }
                else throw new Exception("Not Found");
            }
        }
Пример #7
0
        public List<BeerViewModel> Top(BeerStyle? style)
        {
            using (var context = new BeerBoutiqueEntities()) {
                var t = context.Ratings.GroupBy(x => x.Beer).Select(y => new
                {
                    BeerID = y.Key.ID,
                    AverageOverall = y.Average(z => z.Overall)
                }).OrderBy(x => x.AverageOverall).Take(10);

                var b = new List<BeerViewModel>();
                foreach (var a in t) {
                    b.Add(new BeerViewModel(context.Beers.SingleOrDefault(x => x.ID == a.BeerID)));
                }

                //var beers = context.Beers.Join(t, Beers => Beers.ID, Ratings => Ratings.BeerID, (beer, rating) => new BeerViewModel(beer)).ToList();

                return b.OrderByDescending(x => x.AverageOverall).ToList();
            }
        }
Пример #8
0
        public ReviewViewModel GetReview(int beerId, int userId)
        {
            if (beerId <= 0)
            {
                throw new ArgumentNullException("beerId");
            }
            if (userId <= 0)
            {
                throw new ArgumentNullException("userId");
            }

            using (var context = new BeerBoutiqueEntities()) {
                var reviews = context.Reviews.FirstOrDefault(x => x.BeerID == beerId && x.UserID == userId);
                if (reviews != null) {
                    return new ReviewViewModel(reviews);
                }
            }

            return null;
        }
Пример #9
0
        public bool Update(BreweryViewModel b)
        {
            using (var context = new BeerBoutiqueEntities())
            {
                var brewery = (from x in context.Breweries
                               where x.ID == b.ID
                               select x).FirstOrDefault();

                if (brewery == null)
                    return false;

                brewery.Description = b.Description;
                brewery.Name = b.Name;
                brewery.Contact.Address.Locality = b.Locality;
                brewery.Contact.Address.Region = b.Region;
                brewery.Contact.Phone = b.Phone;
                brewery.Contact.Website = b.Uri;
                context.SaveChanges();
                return true;
            }
        }
Пример #10
0
        public bool Rate(int beerId, double score, int userId)
        {
            using (var context = new BeerBoutiqueEntities()) {

                var currentRating = context.Ratings.FirstOrDefault(x => x.BeerID == beerId && x.UserID == userId);

                if (currentRating == null) {
                    context.Ratings.Add(new Rating()
                    {
                        BeerID = beerId,
                        Overall = score,
                        UserID = userId
                    });
                }
                else {
                    currentRating.Overall = score;
                }

                context.SaveChanges();
                return true;
            }
        }
Пример #11
0
 public Rating Get(int beerId, int userId)
 {
     using (var context = new BeerBoutiqueEntities()) {
         return context.Ratings.FirstOrDefault(x => x.BeerID == beerId && x.UserID == userId);
     }
 }
Пример #12
0
 public IEnumerable<SearchAllAzure_Result> Search(string query, string userID = null, int limit = 15)
 {
     using (var context = new BeerBoutiqueEntities()) {
         return context.SearchAllAzure(query, userID).Take(limit).ToList();
     }
 }
Пример #13
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated ||
                !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider,
                                                               out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (var membershipEntities = new BeerBoutiqueEntities())
                {
                    var user = membershipEntities.Users.FirstOrDefault(x => x.UserName.ToLower() == model.UserName.ToLower());

                    if (user == null)
                    {
                        membershipEntities.Users.Add(new User()
                        {
                            UserName = model.UserName
                        });

                        membershipEntities.SaveChanges();
                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName",
                                                 "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Пример #14
0
        public bool Update(BeerViewModel b)
        {
            using (var context = new BeerBoutiqueEntities()) {

                var oldBeer = (from x in context.Beers
                               where x.ID == b.ID
                               select x).FirstOrDefault();

                if (oldBeer == null) {
                    return false;
                }

                oldBeer.StyleID = b.StyleID;
                oldBeer.IBU = b.IBU;
                oldBeer.ABV = b.ABV;
                oldBeer.Description = b.Description;
                oldBeer.Name = b.Name;
                oldBeer.SRM = b.SRM;
                context.SaveChanges();
                return true;
            }
        }