public void updateKennelAvailability(DateTime date, string id, Kennel kennel)
        {
            //for the date the loop is on in the iteration it will check to see if the date already exists
            bool dateExists = db.KennelAvailability.Where(k => k.KennelID.ToUpper() == id.ToUpper() && k.BookingDate == date).Count() > 0;

            //if the date does not exist it will add it to the table
            if (dateExists == false)
            {
                var newDate = new KennelAvailability()
                {
                    KennelID     = id.ToUpper(),
                    BookingDate  = date,
                    Availability = 1
                };
                db.KennelAvailability.Add(newDate);
            }
            //if the date does exists it will update the existing entry.
            else
            {
                //Adds 1 to Availability on the KennelAvailability table where the KennelId and Date match those of the booking.
                var ka = db.KennelAvailability.Where(r => r.KennelID.ToUpper() == id.ToUpper() && r.BookingDate == date).First();
                ka.Availability   += 1;
                db.Entry(ka).State = EntityState.Modified;


                //Updates the KennelAvailability to full if the kennel has reached capacity for that date.
                if (ka.Availability == kennel.Capacity)
                {
                    ka.Full            = true;
                    db.Entry(ka).State = EntityState.Modified;
                }
            }
        }
        public async Task <ActionResult> Create(string id, [Bind(Include = "RatingID,Ratings,KennelID,Comment,RatingDate")] Rating rating)
        {
            var currentUser = getUser();

            if (ModelState.IsValid)
            {
                bool totalRatingExists = db.TotalRating.Where(k => k.KennelID == id).Count() > 0;

                //if the kennel has never been rated it will add an initial rating
                if (totalRatingExists == false)
                {
                    var newTRate = new TotalRating()
                    {
                        KennelID      = id,
                        TotalRatings  = rating.Ratings,
                        TotalRaters   = 1,
                        AverageRating = rating.Ratings
                    };

                    db.TotalRating.Add(newTRate);
                    await db.SaveChangesAsync();
                }

                //if the kennel has been rated previously (by any user) it will update the existing entry.
                else
                {
                    //Adds 1 to total Raters on the TotalRatings table where the KennelId match those of the rating.
                    //Adds the rating to the TotalRatings.
                    //Updates AverageRating
                    var tra = db.TotalRating.Where(r => r.KennelID == id).First();
                    tra.TotalRaters    += 1;
                    tra.TotalRatings   += rating.Ratings;
                    tra.AverageRating   = tra.calcAvgRating(tra.TotalRatings, tra.TotalRaters);
                    db.Entry(tra).State = EntityState.Modified;
                    await db.SaveChangesAsync();
                }

                rating.RatingDate = DateTime.Now;
                rating.User       = currentUser;
                rating.KennelID   = id;
                db.Rating.Add(rating);
                await db.SaveChangesAsync();

                TempData["Thank"] = "Thank you for your rating";
                return(RedirectToAction("Index"));
            }

            return(View(rating));
        }
Example #3
0
        public async Task <ActionResult> Edit([Bind(Include = "KennelID,Name,Address,County,Town,PhoneNumber,Email,Capacity,PricePerNight,PricePerWeek,MaxDays,LgeDog,MedDog,SmlDog,CancellationPeriod,Description,Grooming,Training")] Kennel kennels)
        {
            if (ModelState.IsValid)
            {
                //Capitalises to title case when updated
                kennels.Name    = TitleCase(kennels.Name);
                kennels.Address = TitleCase(kennels.Address);
                kennels.Town    = TitleCase(kennels.Town);

                //Updates the database entry
                db.Entry(kennels).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("MyKennels"));
            }
            return(View(kennels));
        }
        public async Task <ActionResult> Edit(EditUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var Db     = new KennelsContext();
                var userId = User.Identity.GetUserId();
                var user   = Db.Users.SingleOrDefault(u => u.Id == userId);
                //Updates the data changed by the user.
                user.Fname       = model.Fname;
                user.Lname       = model.Lname;
                user.Address     = model.Address;
                user.County      = model.County;
                user.PhoneNumber = model.PhoneNumber;

                Db.Entry(user).State = EntityState.Modified;
                await Db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            // Redisplays form if some input not valid.
            return(View(model));
        }