public async Task MakeReservation(PaymentDTO car) { LocationDTO pickUpLocation = await locationManager.GetLocation(car.PickUpLocationId); LocationDTO dropDownLocation = await locationManager.GetLocation(car.DropDownLocationId); SearchDTO selectedCar = new SearchDTO { Id = car.Id, ReserveFrom = car.ReserveFrom, ReserveTo = car.ReserveTo, CountryPickUp = pickUpLocation.Country, CityPickUp = pickUpLocation.City, LocationPickUp = pickUpLocation.Address, CountryDropDown = dropDownLocation.Country, CityDropDown = dropDownLocation.City, LocationDropDown = dropDownLocation.Address }; List <int> avaibleCarIds = carManager.GetCars(selectedCar).Select(c => c.Id).ToList(); //check if still available if (avaibleCarIds.Contains(car.Id)) { string userId = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); CarDTO originalCar = await carManager.GetCar(car.Id); await _context.AddAsync(new Reservation { CarId = car.Id, ReservationPrice = ((int)Math.Abs((car.ReserveFrom - car.ReserveTo).TotalDays) + 1) * originalCar.Price, ReserveFrom = car.ReserveFrom, ReserveTo = car.ReserveTo, PickUpLocationId = pickUpLocation.Id, DropDownLocationId = dropDownLocation.Id, UserId = userId }); await _context.SaveChangesAsync(); } }
public async Task AddLocation(LocationDTO location) { await _context.Locations .AddAsync(new Location { Country = location.Country, City = location.City, Address = location.Address, IsGlobal = location.IsGlobal, CompanyId = location.CompanyId }); await _context.SaveChangesAsync(); }
public async Task AddAdminForCompany(int companyId, string userId) { CompanyAdmin companyAdmin = await _context.CompanyAdmins .Where(ca => ca.CompanyId == companyId && ca.UserId == userId) .FirstOrDefaultAsync(); if (companyAdmin != null) { return; } await _context.CompanyAdmins .AddAsync(new CompanyAdmin { UserId = userId, CompanyId = companyId }); await _context.SaveChangesAsync(); }
public async Task <IActionResult> Remove(int id) { var reservationsToDelete = await _context.Reservations.Where(r => r.CarId == id).ToListAsync(); foreach (Reservation r in reservationsToDelete) { _context.Reservations.Remove(r); } Car car = await _context.Cars.Where(c => c.Id == id).FirstOrDefaultAsync(); if (car != null) { _context.Cars.Remove(car); } await _context.SaveChangesAsync(); return(Ok(id)); }
public async Task <IActionResult> Remove(int id) { var companyCars = await _context.Cars.Where(c => c.CompanyId == id).ToListAsync(); foreach (Car car in companyCars) { var reservationsToDelete = await _context.Reservations.Where(r => r.CarId == car.Id).ToListAsync(); foreach (Reservation r in reservationsToDelete) { _context.Reservations.Remove(r); } _context.Cars.Remove(car); } var companyLocations = await _context.Locations .Where(c => c.CompanyId == id && c.IsGlobal == false) .ToListAsync(); foreach (Location location in companyLocations) { _context.Locations.Remove(location); } var company = await _context.Companies.Where(c => c.Id == id).FirstOrDefaultAsync(); if (company != null) { _context.Companies.Remove(company); } await _context.SaveChangesAsync(); return(Ok(id)); }
public async Task AddNewCarAsync(Car c) { await _context.Cars.AddAsync(c); await _context.SaveChangesAsync(); }