コード例 #1
0
ファイル: HomeController.cs プロジェクト: Artyryr/COMP231
        public async Task <ActionResult> ServiceBookingPage(ServiceRequestModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //Logic for Authentificated User
                    if (User.Identity.IsAuthenticated)
                    {
                        GeneralUser user = await GetCurrentUserAsync();

                        Service          service          = repository.Services.Where(p => p.ServiceId == model.RequestedService.ServiceId).FirstOrDefault();
                        RequestedService requestedService = model.RequestedService;
                        Payment          payment          = model.Payment;
                        payment.UserId = user.Id;

                        requestedService.AddPayment(payment);
                        requestedService.UserId = user.Id;

                        //Calculates the resulted price after discount for registered user
                        requestedService.TotalPrice = requestedService.NumberOfHours * service.PricePerHour - (requestedService.NumberOfHours * service.PricePerHour * user.Discount / 100);

                        repository.AddPayment(payment);
                        repository.AddRequestedService(requestedService);

                        model.Discount         = user.Discount;
                        model.RequestedService = requestedService;
                        model.Service          = service;
                        model.User             = user;

                        return(RedirectToAction("BookingConfirmationPage", new BookingConfirmationModel {
                            Discount = model.Discount, ServiceId = model.Service.ServiceId, RequestedServiceId = model.RequestedService.RequestedServiceId, PaymentId = model.Payment.PaymentId
                        }));
                    }
                    //Logic for unregistered user
                    else
                    {
                        Service          service          = repository.Services.Where(p => p.ServiceId == model.RequestedService.ServiceId).FirstOrDefault();
                        RequestedService requestedService = model.RequestedService;
                        Payment          payment          = model.Payment;

                        requestedService.AddPayment(payment);

                        //Calculates resulted price of a requested service
                        requestedService.TotalPrice = requestedService.NumberOfHours * service.PricePerHour;

                        repository.AddRequestedService(requestedService);
                        model.RequestedService = requestedService;
                        model.Service          = service;

                        return(RedirectToAction("BookingConfirmationPage", new BookingConfirmationModel {
                            ServiceId = model.Service.ServiceId, RequestedServiceId = model.RequestedService.RequestedServiceId, PaymentId = model.Payment.PaymentId
                        }));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return(View(model));
                }
            }
            else
            {
                return(View(model));
            }
        }