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));
        }
        public async Task <ActionResult> Create(string id, [Bind(Include = "BookingID,StartDate,EndDate,PhoneNumber,PetsName,TotalNights,Price,KennelID")] Booking booking)
        {
            var kennel      = db.Kennel.Where(k => k.KennelID.ToUpper() == id.ToUpper()).First();
            var currentUser = getUser();

            booking = TempData["confirm"] as Booking;

            if (TempData["confirm"] != null)
            {
                //Loops through the dates in the booking from start to end
                for (DateTime date = booking.StartDate; date <= booking.EndDate; date = date.AddDays(1))
                {
                    updateKennelAvailability(date, id, kennel);
                    await db.SaveChangesAsync();
                }

                //Calls the funtion to calculate the total nights based on the start and end date specified by the user
                booking.TotalNights = booking.CalcTotalNights(booking.StartDate, booking.EndDate);
                booking.PhoneNumber = currentUser.PhoneNumber;
                //Calls the function CalcTotalPrice which takes in (double pricePerNight, double pricePerWeek) to calculate the price based on the prices the kennel offers.
                booking.Price    = booking.CalcTotalPrice(kennel.PricePerNight, kennel.PricePerWeek, booking.TotalNights);
                booking.KennelID = id;
                booking.User     = currentUser;
                db.Booking.Add(booking);

                await db.SaveChangesAsync();

                //Body of the email
                string bookingConf = "<p> Kennel Name: " + kennel.Name + "</p><p>For pet: " + booking.PetsName + "</p><p> Date from: " + booking.StartDate.ToString().Substring(0, 10) +
                                     "</p><p> Date to: " + booking.EndDate.Date.ToString().Substring(0, 10) + "</p><p> Total price: " + booking.Price;
                string Message = "<p>Hi " + currentUser.Fname + " " + currentUser.Lname + "</p>" +
                                 "<p>Thank you for booking with IS Kennels. </p> <p>Your booking is as follows: </p>" + bookingConf;
                string Subject = "Booking Conformation for " + kennel.Name;
                string Email   = currentUser.Email;

                //Calls the email funtion
                SendConfirmation(Message, Subject, Email);

                TempData["Thank"] = "Booking confirmed. Thank you for your booking";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["Oops"] = "Oops something has gone wrong please try again";
                return(RedirectToAction("CreateViewModel", new { id = id }));
            }
        }
Example #3
0
        public async Task <ActionResult> Create([Bind(Include = "KennelID,Name,Address,County,Town,PhoneNumber,Email,Capacity,PricePerNight,PricePerWeek,MaxDays,LgeDog,MedDog,SmlDog,CancellationPeriod,Description,Grooming,Training")] Kennel kennels)
        {
            var currentUser = GetUser();

            if (ModelState.IsValid)
            {
                kennels.User = currentUser;

                //Capitalises in title case when added to the database
                kennels.KennelID = kennels.KennelID.ToUpper();
                kennels.Name     = TitleCase(kennels.Name);
                kennels.Address  = TitleCase(kennels.Address);
                kennels.Town     = TitleCase(kennels.Town);

                //Adds the kennel to the database
                db.Kennel.Add(kennels);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            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));
        }