public async Task <IActionResult> AssignRacCompanyToUser(RaCAssignmentModel racAssignmentModel) { using (var transaction = _dbContext.Database.BeginTransaction()) { var owner = _dbContext.Users.SingleOrDefault(u => u.Email == racAssignmentModel.OwnerEmail); if (owner == null) { return(Ok(new { message = "User with given email does not exist!" })); } if (owner.RaCCompany != null) { return(Ok(new { message = "User already owns another company!" })); } RentACarCompany rac = new RentACarCompany(); rac.CompanyName = racAssignmentModel.CompanyName; rac.Offices = new Collection <Office>(); owner.RaCCompany = rac; owner.Role = UserRole.CarAdmin; _dbContext.SaveChanges(); transaction.Commit(); return(Ok(new { message = "New rent a car administrator is successfully registered!" })); } }
public async Task <ActionResult <RentACarCompany> > PostRentACarCompany(RentACarCompany rentACarCompany) { _context.RentACarCompanies.Add(rentACarCompany); await _context.SaveChangesAsync(); return(CreatedAtAction("GetRentACarCompany", new { id = rentACarCompany.Id }, rentACarCompany)); }
public async Task <IActionResult> GetRacCompanyCars(IdModel racModel) //Koristimo samo da smjestimo id { RentACarCompany rac = new RentACarCompany(); using (var transaction = _dbContext.Database.BeginTransaction()) { rac = _dbContext.RentACarCompanies.Include(x => x.Offices).ThenInclude(x => x.Cars).Where(x => x.Id == Int32.Parse(racModel.Id)).SingleOrDefault(); transaction.Commit(); } if (rac == null) { return(Ok(new { message = "Rac company does not exist!" })); } List <Car> retCars = new List <Car>(); if (rac != null) { foreach (var office in rac.Offices) { foreach (var car in office.Cars) { retCars.Add(car); } } return(Ok(new { retCars })); } else { return(Ok(new { retCars = "Rent a Car Company with this id does not exist!" })); } }
public async Task <IActionResult> GetRacProfileInfo(UserIdModel userIdModel) { using (var transaction = _dbContext.Database.BeginTransaction()) { User user = _dbContext.Users.Include(c => c.RaCCompany).SingleOrDefault(c => c.Id == userIdModel.OwnerId); //nece da radi bez include if (user == null) { return(Ok("User with given id is not registered!")); } if (user.Role.ToString() != UserRole.CarAdmin.ToString()) { return(Unauthorized("User does not have permission to use this method!")); } RentACarCompany racCompany = _dbContext.RentACarCompanies.Where(x => x.Id == user.RaCCompany.Id).SingleOrDefault(); if (racCompany == null) { return(Ok("Rent a car company does not exist!")); } transaction.Commit(); return(Ok(new { racCompany })); } }
public async Task <IActionResult> GetUserOffices(UserIdModel userIdModel) { using (var transaction = _dbContext.Database.BeginTransaction()) { User user = _dbContext.Users.Include(c => c.RaCCompany).SingleOrDefault(c => c.Id == userIdModel.OwnerId); if (user == null) { return(Ok("User with given id is not registered!")); } if (user.Role.ToString() != UserRole.CarAdmin.ToString()) { return(Unauthorized("User does not have permission to use this method!")); } RentACarCompany racCompany = _dbContext.RentACarCompanies.Include(x => x.Offices).SingleOrDefault(x => x.Id == user.RaCCompany.Id); if (racCompany == null) { return(Ok("Rent a car company does not exist!")); } //List<Office> allOffices = _dbContext.Offices.Where(x => x) List <Office> allOffices = racCompany.Offices.ToList(); transaction.Commit(); return(Ok(new { allOffices })); } }
public async Task <IActionResult> RegisterNewOffice(RegisterNewOfficeModel registerNewOfficeModel) { using (var transaction = _dbContext.Database.BeginTransaction()) { User user = _dbContext.Users.Include(c => c.RaCCompany).SingleOrDefault(c => c.Id == registerNewOfficeModel.OwnerId); if (user == null) { return(Ok(new { message = "User with given id is not registered!" })); } if (user.Role.ToString() != UserRole.CarAdmin.ToString()) { return(Unauthorized(new { message = "User does not have permission to use this method!" })); } RentACarCompany racCompany = _dbContext.RentACarCompanies.Include(x => x.Offices).SingleOrDefault(x => x.Id == user.RaCCompany.Id); if (racCompany == null) { return(Ok(new { message = "Rent a car company does not exist!" })); } Office newOffice = new Office(); newOffice.Address = registerNewOfficeModel.Address; newOffice.City = registerNewOfficeModel.City; newOffice.Cars = new Collection <Car>(); racCompany.Offices.Add(newOffice); _dbContext.SaveChanges(); transaction.Commit(); return(Ok(new { message = "New office is successfully registered!" })); } }
public async Task <IActionResult> SaveRacProfileChanges(RacProfileEditModel racProfileEditModel) { User user = new User(); using (var transaction = _dbContext.Database.BeginTransaction()) { user = _dbContext.Users.Include(c => c.RaCCompany).SingleOrDefault(c => c.Id == racProfileEditModel.OwnerId); //nece da radi bez include if (user == null) { return(Ok(new { message = "User with given id is not registered!" })); } if (user.Role.ToString() != UserRole.CarAdmin.ToString()) { return(Unauthorized(new { message = "User does not have permission to use this method!" })); } RentACarCompany comp = _dbContext.RentACarCompanies.Where(x => x.Id == user.RaCCompany.Id).SingleOrDefault(); if (comp == null) { return(Ok(new { message = "Rent a car company does not exist!" })); } if (racProfileEditModel.CompanyName != "") { comp.CompanyName = racProfileEditModel.CompanyName; } if (racProfileEditModel.Adress != "") { comp.Adress = racProfileEditModel.Adress; } if (racProfileEditModel.Description != "") { comp.Description = racProfileEditModel.Description; } if (racProfileEditModel.PhoneNumber != "") { comp.PhoneNumber = racProfileEditModel.PhoneNumber; } _dbContext.SaveChanges(); transaction.Commit(); } return(Ok(new { message = "Rac profile is successfully updated!" })); }
public async Task <IActionResult> GetRacCompanyOffices(IdModel racModel) //Koristimo samo da smjestimo id { RentACarCompany rac = new RentACarCompany(); using (var transaction = _dbContext.Database.BeginTransaction()) { rac = _dbContext.RentACarCompanies.Include(x => x.Offices).Where(x => x.Id == Int32.Parse(racModel.Id)).SingleOrDefault(); transaction.Commit(); } if (rac != null) { List <Office> retOffices = rac.Offices.ToList(); return(Ok(new { retOffices })); } else { return(Ok(new { retOffices = "Rent a Car Company with this id does not exist!" })); } }
public async Task <IActionResult> GetRacCompanyRating(IdModel racModel) //Koristimo samo da smjestimo id { int retVal = 0; RentACarCompany rac = new RentACarCompany(); using (var transaction = _dbContext.Database.BeginTransaction()) { rac = _dbContext.RentACarCompanies.Include(x => x.Offices).ThenInclude(x => x.Cars).ThenInclude(x => x.CarReservations).Where(x => x.Id == Int32.Parse(racModel.Id)).SingleOrDefault(); transaction.Commit(); } if (rac != null) { int rating = 0; int numberOfRatings = 0; foreach (var office in rac.Offices) { foreach (var car in office.Cars) { rating += car.CarRating; numberOfRatings += car.NumberOfRatings; } } if (numberOfRatings != 0) { retVal = rating / numberOfRatings; } return(Ok(new { retVal })); } else { return(Ok(new { retVal = "Rent a Car Company with this id does not exist!" })); } }
public async Task <IActionResult> PutRentACarCompany(int id, RentACarCompany rentACarCompany) { if (id != rentACarCompany.Id) { return(BadRequest()); } var racCompany = await _context.RentACarCompanies .Include(company => company.Branches) .Include(company => company.Services) .Include(company => company.Vehicles) .ThenInclude(vehicle => vehicle.FreeDates) .FirstOrDefaultAsync(x => x.Id == id); // update properties on the parent _context.Entry(racCompany).CurrentValues.SetValues(rentACarCompany); #region Branches // remove or update child collection items var branches = racCompany.Branches.ToList(); foreach (var branch in branches) { var b = rentACarCompany.Branches.SingleOrDefault(x => x.Id == branch.Id); if (b != null) { _context.Entry(branch).CurrentValues.SetValues(b); } else { _context.Branches.Remove(branch); } } // add the new items foreach (var branch in rentACarCompany.Branches) { if (branches.All(i => i.Id != branch.Id)) { racCompany.Branches.Add(branch); } } #endregion #region Services // remove or update child collection items var services = racCompany.Services.ToList(); foreach (var service in services) { var s = rentACarCompany.Services.SingleOrDefault(x => x.Id == service.Id); if (s != null) { _context.Entry(service).CurrentValues.SetValues(s); } else { _context.Remove(service); } } // add the new items foreach (var service in rentACarCompany.Services) { if (services.All(i => i.Id != service.Id)) { racCompany.Services.Add(service); } } #endregion // Kada se vrsi azuriranje RentACarCompany-je, Vehicle se samo dodaje #region Vehicles var vehicles = racCompany.Vehicles.ToList(); // add the new items foreach (var vehicle in rentACarCompany.Vehicles) { if (vehicles.All(i => i.Id != vehicle.Id)) { racCompany.Vehicles.Add(vehicle); } } #endregion try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RentACarCompanyExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }