예제 #1
0
        public void Execute(int id, RoleRequestDTO roleDTO)
        {
            var role = AiContext.Roles.Find(id);

            if (role == null || role.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Role");
            }
            var roleExists = AiContext.Roles
                             .Where(x => x.Name == roleDTO.Name)
                             .Where(x => x.IsDeleted == 0)
                             .FirstOrDefault();

            if (roleExists != null)
            {
                throw new EntityExistsException("Role with that name");
            }
            role.Name       = roleDTO.Name;
            role.ModifiedAt = DateTime.UtcNow;
            AiContext.SaveChanges();
        }
        public UserDTO Execute(UserRequestDTO request)
        {
            var user     = new User();
            var role     = new Role();
            var username = AiContext.Users
                           .Where(u => u.Username == request.Username)
                           .FirstOrDefault();

            if (username != null && username.IsDeleted == 0)
            {
                throw new EntityExistsException("User with that username");
            }
            int roleId = request.RoleId.HasValue ? request.RoleId.GetValueOrDefault() : 8;

            role = AiContext.Roles
                   .Where(x => x.Id == roleId)
                   .Where(x => x.IsDeleted == 0)
                   .FirstOrDefault();
            if (role == null)
            {
                throw new EntityNotFoundException("Role");
            }
            user.FirstName = request.FirstName;
            user.LastName  = request.LastName;
            user.Username  = request.Username;
            user.Password  = request.Password;
            user.Role      = role;
            AiContext.Users.Add(user);
            AiContext.SaveChanges();
            return(new UserDTO
            {
                Id = user.Id,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Username = user.Username,
                Password = user.Password,
                RoleId = user.RoleId,
                RoleName = role.Name
            });
        }
예제 #3
0
        public VehicleResponseDTO Execute(VehicleRequestDTO request)
        {
            var vehicle = new Vehicle();
            var brand   = AiContext.Brands
                          .Find(request.BrandId);

            if (brand == null)
            {
                throw new EntityNotFoundException("Brand");
            }
            vehicle.BrandId = request.BrandId;
            var vehicleType = AiContext.VehicleTypes
                              .Find(request.VehicleTypeId);

            if (vehicleType == null)
            {
                throw new EntityNotFoundException("Vehicle type");
            }
            vehicle.Automatic        = request.Automatic.HasValue ? request.Automatic.Value : false;
            vehicle.VehicleTypeId    = request.VehicleTypeId;
            vehicle.Model            = request.Model;
            vehicle.CostPerDay       = request.CostPerDay;
            vehicle.FuelTankCapacity = request.FuelTankCapacity;
            vehicle.Color            = request.Color;
            AiContext.Vehicles.Add(vehicle);
            AiContext.SaveChanges();
            return(new VehicleResponseDTO
            {
                Id = vehicle.Id,
                Model = vehicle.Model,
                CostPerDay = vehicle.CostPerDay,
                Color = vehicle.Color,
                FuelTankCapacity = vehicle.FuelTankCapacity,
                VehicleType = vehicle.VehicleType.Name,
                VehicleBrand = vehicle.Brand.Name,
                Automatic = vehicle.Automatic,
                Rented = vehicle.Rented
            });
        }
        public void Execute(int search, BrandRequestDTO request)
        {
            var brand = AiContext.Brands.Find(search);

            if (brand == null || brand.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Brand");
            }
            var brandExists = AiContext.Brands
                              .Where(x => x.Name == request.Name)
                              .Where(x => x.Id != brand.Id)
                              .Where(x => x.IsDeleted == 0)
                              .FirstOrDefault();

            if (brandExists != null)
            {
                throw new EntityExistsException("Brand");
            }
            brand.Name       = request.Name;
            brand.ModifiedAt = DateTime.Now;
            AiContext.SaveChanges();
        }
        public VehicleTypeResponseDTO Execute(VehicleTypeRequestDTO request)
        {
            var vehType         = new VehicleType();
            var existingVehType = AiContext.VehicleTypes
                                  .Where(x => x.Name == request.Name)
                                  .Where(x => x.IsDeleted == 0)
                                  .FirstOrDefault();

            if (existingVehType != null)
            {
                throw new EntityExistsException("Vehicle type");
            }
            vehType.Name = request.Name;
            AiContext.VehicleTypes
            .Add(vehType);
            AiContext.SaveChanges();
            return(new VehicleTypeResponseDTO
            {
                Id = vehType.Id,
                Name = vehType.Name
            });
        }
예제 #6
0
        public void Execute(int search, StatusRequestDTO request)
        {
            var status = AiContext.RentStatus
                         .Find(search);

            if (status == null || status.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Rent status");
            }
            var existingStatus = AiContext.RentStatus
                                 .Where(x => x.Name == request.Name)
                                 .Where(x => x.IsDeleted == 0)
                                 .FirstOrDefault();

            if (existingStatus != null)
            {
                throw new EntityExistsException("Rent status");
            }
            status.Name       = request.Name;
            status.ModifiedAt = DateTime.Now;
            AiContext.SaveChanges();
        }
예제 #7
0
        public void Execute(int search, UserUpdateRequestDTO request)
        {
            var role     = new Role();
            var username = AiContext.Users
                           .Where(u => u.Username == request.Username)
                           .Where(u => u.IsDeleted == 0)
                           .Where(u => u.Id != search)
                           .FirstOrDefault();
            var user = AiContext.Users
                       .Where(u => u.Id == search)
                       .Where(u => u.IsDeleted == 0)
                       .FirstOrDefault();

            if (user == null)
            {
                throw new EntityNotFoundException("User");
            }
            if (username != null)
            {
                throw new EntityExistsException("User with that username");
            }
            int roleId = request.RoleId.HasValue ? request.RoleId.GetValueOrDefault() : 8;

            role = AiContext.Roles
                   .Where(x => x.Id == roleId)
                   .Where(x => x.IsDeleted == 0)
                   .FirstOrDefault();
            if (role == null)
            {
                throw new EntityNotFoundException("Role");
            }
            user.FirstName  = request.FirstName;
            user.LastName   = request.LastName;
            user.Username   = request.Username;
            user.RoleId     = roleId;
            user.ModifiedAt = DateTime.Now;
            AiContext.SaveChanges();
        }
예제 #8
0
        public LocationResponseDTO Execute(LocationRequestDTO request)
        {
            var location         = new Location();
            var existingLocation = AiContext.Locations
                                   .Where(x => x.Adress == request.Adress)
                                   .Where(x => x.IsDeleted == 0)
                                   .FirstOrDefault();

            if (existingLocation != null)
            {
                throw new EntityExistsException("Location");
            }
            location.Adress = request.Adress;
            location.Price  = request.Price;
            AiContext.Add(location);
            AiContext.SaveChanges();
            return(new LocationResponseDTO
            {
                Id = location.Id,
                Adress = location.Adress,
                Price = location.Price
            });
        }
        public Brand Execute(BrandRequestDTO request)
        {
            var brand         = new Brand();
            var existingBrand = AiContext.Brands.Where(x => x.Name == request.Name).Where(x => x.IsDeleted == 0).FirstOrDefault();

            if (existingBrand != null)
            {
                throw new EntityExistsException("Brand");
            }
            brand.Name = request.Name;
            AiContext.Brands.Add(brand);
            AiContext.SaveChanges();
            _emailSender.Body    = "Your rent has ended, thank you for using our service!";
            _emailSender.Subject = "Rent reservation";
            _emailSender.ToEmail = "*****@*****.**";
            _emailSender.Send();
            return(brand);
            //return new BrandResponseDTO
            //{
            //    Id = brand.Id,
            //    Name = brand.Name
            //};
        }
예제 #10
0
        public void Execute(int request)
        {
            var rent = AiContext.Rents
                       .Find(request);

            if (rent == null || rent.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Rent");
            }
            //Only allowig if status is "Reserved"
            if (rent.StatusId != 9)
            {
                throw new IndexOutOfRangeException();
            }
            //In progress status, disabling modifying
            rent.StatusId   = 10;
            rent.ModifiedAt = DateTime.Now;
            AiContext.SaveChanges();
            _emailSender.Body    = "Your renting has started";
            _emailSender.Subject = "Rent reservation started";
            _emailSender.ToEmail = rent.Email;
            _emailSender.Send();
        }
        public void Execute(int search, VehicleTypeRequestDTO request)
        {
            var vehType = AiContext.VehicleTypes
                          .Find(search);

            if (vehType == null || vehType.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Vehicle type");
            }
            var existingVehType = AiContext.VehicleTypes
                                  .Where(x => x.Id != vehType.Id)
                                  .Where(x => x.Name == request.Name)
                                  .Where(x => x.IsDeleted == 0)
                                  .FirstOrDefault();

            if (existingVehType != null)
            {
                throw new EntityExistsException("Vehicle type");
            }
            vehType.Name       = request.Name;
            vehType.ModifiedAt = DateTime.Now;
            AiContext.SaveChanges();
        }
예제 #12
0
        public void Execute(int request)
        {
            var rent = AiContext.Rents
                       .Find(request);

            if (rent == null || rent.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Rent");
            }
            if (rent.StatusId == 10)
            {
                throw new EntityExistsException();
            }
            var extraAddonRents = AiContext.RentExtraAddons
                                  .Where(x => x.RentId == request);

            foreach (var x in extraAddonRents)
            {
                x.IsDeleted = 1;
            }
            rent.IsDeleted  = 1;
            rent.ModifiedAt = DateTime.Now;
            AiContext.SaveChanges();
        }
        public void Execute(int search, LocationRequestDTO request)
        {
            var location = AiContext.Locations
                           .Find(search);

            if (location == null || location.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Location");
            }
            var existingLocation = AiContext.Locations
                                   .Where(x => x.Adress == request.Adress)
                                   .Where(x => x.IsDeleted == 0)
                                   .Where(x => x.Id != location.Id)
                                   .FirstOrDefault();

            if (existingLocation != null)
            {
                throw new EntityExistsException("Location");
            }
            location.Adress     = request.Adress;
            location.Price      = request.Price;
            location.ModifiedAt = DateTime.UtcNow;
            AiContext.SaveChanges();
        }
        public void Execute(int search, RentUpdateRequestDTO request)
        {
            var rent = AiContext.Rents
                       .Find(search);

            if (rent == null || rent.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Rent");
            }
            //Allowing modifying rent only in period of reservation
            //Status with id 9 is "Reserved" status
            if (rent.StatusId != 9)
            {
                throw new IndexOutOfRangeException();
            }
            //
            var user = AiContext.Users
                       .Find(request.UserId);

            if (user == null || user.IsDeleted == 1)
            {
                throw new EntityNotFoundException("User");
            }
            var pickLocaton = AiContext.Locations
                              .Find(request.LocationId);

            if (pickLocaton == null || pickLocaton.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Pick up location");
            }
            var dropLocation = AiContext.Locations
                               .Find(request.DropLocationId);

            if (dropLocation == null || dropLocation.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Drop off location");
            }
            var customer = AiContext.Customers
                           .Find(request.CustomerId);

            if (customer == null || customer.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Customer");
            }
            var vehicle = AiContext.Vehicles
                          .Find(request.VehicleId);

            if (vehicle == null || vehicle.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Vehicle");
            }
            //If vehicle is rented renting won't be possible
            if (vehicle.Rented == true && vehicle.Id != rent.VehicleId)
            {
                throw new EntityExistsException();
            }
            //If it's not rented, column in vehicle 'Rented' is changed to true until renting status is 'Returned'
            //Previous vehicle status is set back to avaliable
            if (vehicle.Id != rent.VehicleId)
            {
                vehicle.Rented     = true;
                vehicle.ModifiedAt = DateTime.Now;
                var previousVehicle = AiContext.Vehicles
                                      .Find(rent.VehicleId);
                previousVehicle.Rented     = false;
                previousVehicle.ModifiedAt = DateTime.Now;
            }
            //
            rent.UserId            = request.UserId;
            rent.CustomerId        = request.CustomerId;
            rent.DropLocationId    = request.DropLocationId;
            rent.LocationId        = request.LocationId;
            rent.PickDate          = request.PickDate;
            rent.DropDate          = request.DropDate;
            rent.VehicleId         = request.VehicleId;
            rent.FirstName         = customer.FirstName;
            rent.LastName          = customer.LastName;
            rent.Email             = customer.Email;
            rent.VehicleCostPerDay = vehicle.CostPerDay;
            rent.PickAdress        = pickLocaton.Adress;
            rent.DropAdress        = dropLocation.Adress;
            rent.ModifiedAt        = DateTime.Now;
            //Updating initial price based on vehicle cost, days and pickup location
            var days = Math.Ceiling((request.DropDate - request.PickDate).TotalDays);

            if (days <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            rent.TotallPrice = vehicle.CostPerDay * days + pickLocaton.Price;
            //Updating Extra add-ons
            //Removing extra addons which were in old list and not in new
            var extraAddons = AiContext.RentExtraAddons
                              .Where(x => x.RentId == rent.Id)
                              .Select(x => x.ExtraAddonId);
            var newExtraAddons     = request.ExtraAddonIds;
            var diffOldExtraAddons = extraAddons.Except(newExtraAddons);

            foreach (var x in diffOldExtraAddons)
            {
                var removeExtra = AiContext.RentExtraAddons
                                  .Where(b => b.RentId == rent.Id)
                                  .Where(b => b.ExtraAddonId == x)
                                  .FirstOrDefault();
                AiContext.Remove(removeExtra);
                rent.TotallPrice -= removeExtra.Price;
            }
            //Adding addons which are in new list
            var diffNewExtraAddons = newExtraAddons.Except(extraAddons);

            foreach (var x in diffNewExtraAddons)
            {
                var extra = AiContext.ExtraAddons
                            .Find(x);
                if (extra == null || extra.IsDeleted == 1)
                {
                    throw new EntityNotFoundException("Extra addon with id " + x);
                }
                var extraAddonRent = new RentExtraAddon
                {
                    RentId       = rent.Id,
                    ExtraAddonId = x,
                    Price        = AiContext.ExtraAddons
                                   .Where(e => e.Id == x)
                                   .Select(e => e.Price)
                                   .FirstOrDefault()
                };
                AiContext.RentExtraAddons
                .Add(extraAddonRent);
                rent.TotallPrice += extraAddonRent.Price;
            }
            AiContext.SaveChanges();
        }
        public RentResponseDTO Execute(RentRequestDTO request)
        {
            var rent = new Rent();
            var user = AiContext.Users
                       .Find(request.UserId);

            if (user == null || user.IsDeleted == 1)
            {
                throw new EntityNotFoundException("User");
            }
            var pickLocaton = AiContext.Locations
                              .Find(request.LocationId);

            if (pickLocaton == null || pickLocaton.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Pick up location");
            }
            var dropLocation = AiContext.Locations
                               .Find(request.DropLocationId);

            if (dropLocation == null || dropLocation.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Drop off location");
            }
            int statusId = request.StatusId.HasValue ? request.StatusId.GetValueOrDefault() : 9;
            var status   = AiContext.RentStatus
                           .Find(statusId);

            if (status == null || status.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Rent status");
            }
            var customer = AiContext.Customers
                           .Find(request.CustomerId);

            if (customer == null || customer.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Customer");
            }
            var vehicle = AiContext.Vehicles
                          .Find(request.VehicleId);

            if (vehicle == null || vehicle.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Vehicle");
            }
            //If vehicle is rented renting won't be possible
            if (vehicle.Rented == true)
            {
                throw new EntityExistsException();
            }
            //If it's not rented, column in vehicle 'Rented' is changed to true until renting status is 'Returned'
            vehicle.Rented     = true;
            vehicle.ModifiedAt = DateTime.Now;
            //
            rent.UserId            = request.UserId;
            rent.CustomerId        = request.CustomerId;
            rent.DropLocationId    = request.DropLocationId;
            rent.LocationId        = request.LocationId;
            rent.PickDate          = request.PickDate;
            rent.DropDate          = request.DropDate;
            rent.StatusId          = statusId;
            rent.VehicleId         = request.VehicleId;
            rent.FirstName         = customer.FirstName;
            rent.LastName          = customer.LastName;
            rent.Email             = customer.Email;
            rent.VehicleCostPerDay = vehicle.CostPerDay;
            rent.PickAdress        = pickLocaton.Adress;
            rent.DropAdress        = dropLocation.Adress;
            //Starting calculatin of totall price based on days and vehicle cost per day
            //Price doesn't get lower for durations shorter than day. Price depenends strictly on number of days. 2 hrs = 1 day, 25 hrs = 2 days etc
            var days = Math.Ceiling((request.DropDate - request.PickDate).TotalDays);

            if (days <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            //Getting integer of timespan between drop and pick up date
            rent.TotallPrice = vehicle.CostPerDay * days + pickLocaton.Price;
            //Adding rent to db
            AiContext.Rents.Add(rent);
            //Adding extra addons into db based on array of Extra Addon Ids
            foreach (var x in request.ExtraAddonIds)
            {
                var extra = AiContext.ExtraAddons
                            .Find(x);
                if (extra == null || extra.IsDeleted == 1)
                {
                    throw new EntityNotFoundException("Extra addon with id " + x);
                }
                var extraAddonRent = new RentExtraAddon
                {
                    RentId       = rent.Id,
                    ExtraAddonId = x,
                    Price        = AiContext.ExtraAddons
                                   .Where(e => e.Id == x)
                                   .Select(e => e.Price)
                                   .FirstOrDefault()
                };
                AiContext.RentExtraAddons.Add(extraAddonRent);
                rent.TotallPrice += extraAddonRent.Price;
            }
            //final calculation of price  based on days
            AiContext.SaveChanges();
            _emailSender.Body    = "You have successfully made reservation";
            _emailSender.Subject = "Rent reservation";
            _emailSender.ToEmail = rent.Email;
            _emailSender.Send();
            return(new RentResponseDTO
            {
                Id = rent.Id,
                UserId = rent.UserId,
                CustomerId = rent.CustomerId,
                LocationId = rent.LocationId,
                DropLocationId = rent.DropLocationId,
                VehicleId = rent.VehicleId,
                StatusId = rent.StatusId,
                VehicleModel = vehicle.Model,
                RentStatus = status.Name,
                VehicleCostPerDay = rent.VehicleCostPerDay,
                FirstName = rent.FirstName,
                LastName = rent.LastName,
                Email = rent.Email,
                TotallPrice = rent.TotallPrice,
                PickDate = rent.PickDate,
                DropDate = rent.DropDate,
                PickAdress = pickLocaton.Adress,
                DropAdress = dropLocation.Adress,
                RentExtraAddons = AiContext.RentExtraAddons
                                  .Where(re => re.RentId == rent.Id)
                                  .Select(re => new RentExtraAddonResponseDTO
                {
                    ExtraAddonId = re.ExtraAddonId,
                    ExtraAddonName = re.ExtraAddon.Name,
                    Price = re.ExtraAddon.Price
                })
            });
        }