public async Task <IActionResult> Create(TaxiEntity taxiEntity) { if (ModelState.IsValid) { taxiEntity.Plaque = taxiEntity.Plaque.ToUpper(); _context.Add(taxiEntity); try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "Already exists a Taxi with the same plaque."); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(taxiEntity)); }
public TaxiResponse ToTaxiResponse(TaxiEntity taxiEntity) { return(new TaxiResponse { Id = taxiEntity.Id, Plaque = taxiEntity.Plaque, //Que por cada tripEntities devuelveme TripResponse //Select equivale a un Mapping, El ? que valide los nulos. Trips = taxiEntity.Trips?.Select(t => new TripResponse { EndDate = t.EndDate, Id = t.Id, Qualification = t.Qualification, Remarks = t.Remarks, Source = t.Source, SourceLatitude = t.SourceLatitude, SourceLongitude = t.SourceLongitude, StartDate = t.StartDate, Target = t.Target, TargetLatitude = t.TargetLatitude, TargetLongitude = t.TargetLongitude, TripDetails = t.TripDetails?.Select(td => new TripDetailResponse { Date = td.Date, Id = td.Id, Latitude = td.Latitude, Longitude = td.Longitude }).ToList(), User = ToUserResponse(t.User) //Este user es el que hace el viaje }).ToList(), User = ToUserResponse(taxiEntity.User) //Este user es el conductor del taxi cuando se registra }); }
public async Task <IActionResult> Edit(int id, TaxiEntity taxiEntity) { if (id != taxiEntity.Id) { return(NotFound()); } if (ModelState.IsValid) { taxiEntity.Plaque = taxiEntity.Plaque.ToUpper(); _context.Update(taxiEntity); try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (System.Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "Plaque already exists"); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(taxiEntity)); }
//TODO: Make a popup/toast that show "Added Message and stay on this page for adding more plaques" public async Task <IActionResult> Create(TaxiEntity taxiEntity) { if (ModelState.IsValid) //It must meet the DataNotations Requirements { taxiEntity.Plaque = taxiEntity.Plaque.ToUpper(); _context.Add(taxiEntity); try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (System.Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "Plaque already exists"); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(taxiEntity)); }
public TaxiResponse ToTaxiResponse(TaxiEntity taxiEntity) { return(new TaxiResponse { Id = taxiEntity.Id, Plaque = taxiEntity.Plaque, Trips = taxiEntity.Trips?.Select(t => new TripResponse { EndDate = t.EndDate, Id = t.Id, Qualification = t.Qualification, Remarks = t.Remarks, Source = t.Source, SourceLatitude = t.SourceLatitude, SourceLongitude = t.SourceLongitude, StartDate = t.StartDate, Target = t.Target, TargetLatitude = t.TargetLatitude, TargetLongitude = t.TargetLongitude, TripDetails = t.TripDetails?.Select(td => new TripDetailResponse { Date = td.Date, Id = td.Id, Latitude = td.Latitude, Longitude = td.Longitude }).ToList(), User = ToUserResponse(t.User) }).ToList(), User = ToUserResponse(taxiEntity.User) }); }
public async Task <IActionResult> Edit(int id, TaxiEntity model) { if (id != model.Id) { return(NotFound()); } if (ModelState.IsValid) { model.Plaque = model.Plaque.ToUpper(); _context.Update(model); try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "Already there is a record with the same plaque."); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(model)); }
public async Task <IActionResult> GetTaxiEntity([FromRoute] string plaque) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } plaque = plaque.ToUpper(); TaxiEntity taxiEntity = await _context.Taxis .Include(t => t.User) // Conductor .Include(t => t.Trips) //Vajes .ThenInclude(t => t.TripDetails) .Include(t => t.Trips) .ThenInclude(t => t.User) //Pasajero .FirstOrDefaultAsync(t => t.Plaque == plaque); if (taxiEntity == null) { _context.Taxis.Add(new TaxiEntity { Plaque = plaque }); await _context.SaveChangesAsync(); taxiEntity = await _context.Taxis.FirstOrDefaultAsync(t => t.Plaque == plaque); } return(Ok(_converterHelper.ToTaxiResponse(taxiEntity))); }
private TaxiResponse ToTaxiResponse2(TaxiEntity taxi) { return(new TaxiResponse { Id = taxi.Id, Plaque = taxi.Plaque, User = ToUserResponse(taxi.User) }); }
public async Task <IActionResult> DeleteConfirmed(int id) { TaxiEntity taxiEntity = await _context.Taxis.FindAsync(id); _context.Taxis.Remove(taxiEntity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create([Bind("Id,Plque")] TaxiEntity taxiEntity) { if (ModelState.IsValid) { _context.Add(taxiEntity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(taxiEntity)); }
public async Task <IActionResult> PostTaxiEntity([FromBody] TaxiEntity taxiEntity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.TaxiEntities.Add(taxiEntity); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTaxiEntity", new { id = taxiEntity.Id }, taxiEntity)); }
public async Task <IActionResult> Create(TaxiEntity taxiEntity) { if (ModelState.IsValid) { taxiEntity.Plaque = taxiEntity.Plaque.ToUpper(); _context.Add(taxiEntity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(taxiEntity)); }
public async Task <IActionResult> PostTripEntity([FromBody] TripRequest tripRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } UserEntity userEntity = await _userHelper.GetUserAsync(tripRequest.UserId); if (userEntity == null) { return(BadRequest("User doesn't exists.")); } TaxiEntity taxEntity = await _context.Taxis.FirstOrDefaultAsync(t => t.Plaque == tripRequest.Plaque); if (taxEntity == null) { _context.Taxis.Add(new TaxiEntity { Plaque = tripRequest.Plaque.ToUpper(), }); await _context.SaveChangesAsync(); taxEntity = await _context.Taxis.FirstOrDefaultAsync(t => t.Plaque == tripRequest.Plaque); } TripEntity tripEntity = new TripEntity { Source = tripRequest.Address, SourceLatitude = tripRequest.Latitude, SourceLongitude = tripRequest.Longitude, StartDate = DateTime.UtcNow, Taxi = taxEntity, TripDetails = new List <TripDetailEntity> { new TripDetailEntity { Date = DateTime.UtcNow, Latitude = tripRequest.Latitude, Longitude = tripRequest.Longitude } }, UserEntity = userEntity, }; _context.Trips.Add(tripEntity); await _context.SaveChangesAsync(); return(Ok(_converterHelper.ToTripResponse(tripEntity))); }
// GET: Taxis/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } TaxiEntity taxiEntity = await _context.Taxis.FindAsync(id); if (taxiEntity == null) { return(NotFound()); } return(View(taxiEntity)); }
// GET: Taxis/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } TaxiEntity taxiEntity = await _context.Taxis .FirstOrDefaultAsync(m => m.Id == id); if (taxiEntity == null) { return(NotFound()); } return(View(taxiEntity)); }
public async Task <IActionResult> Edit(int id, TaxiEntity taxiEntity) { if (id != taxiEntity.Id) { return(NotFound()); } if (ModelState.IsValid) { try { taxiEntity.Plate = taxiEntity.Plate.ToUpper(); _context.Update(taxiEntity); try { await _context.SaveChangesAsync(); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "Plate Number Already exist. Please check and try again."); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } return(View(taxiEntity)); } } catch (DbUpdateConcurrencyException) { if (!TaxiEntityExists(taxiEntity.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(taxiEntity)); }
public async Task <IActionResult> Edit(int id, TaxiEntity taxiEntity) { if (id != taxiEntity.Id) { return(NotFound()); } if (ModelState.IsValid) { taxiEntity.Plaque = taxiEntity.Plaque.ToUpper(); _context.Update(taxiEntity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(taxiEntity)); }
// GET: Taxis/Delete/5 public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } TaxiEntity taxiEntity = await _context.Taxis.FindAsync(id); if (taxiEntity == null) { return(NotFound()); } _context.Taxis.Remove(taxiEntity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } TaxiEntity model = await _context.Taxis .FirstOrDefaultAsync(m => m.Id == id); if (model == null) { return(NotFound()); } _context.Taxis.Remove(model); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
// GET: Taxis/Delete/5 public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } TaxiEntity taxiEntity = await _context.Taxis .FirstOrDefaultAsync(m => m.Id == id); if (taxiEntity == null) { return(NotFound()); } _context.Taxis.Remove(taxiEntity); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Taxis")); }
public async Task <ActionResult <TaxiEntity> > GetTaxiEntity(string plate) { plate = plate.ToUpper(); var taxiEntity = await _context.Taxis .Include(t => t.User) //Driver .Include(t => t.Trips) .ThenInclude(t => t.TripDetails) .Include(t => t.Trips) .Include(t => t.User) //Passenger .FirstOrDefaultAsync(t => t.Plate == plate); if (taxiEntity == null) { taxiEntity = new TaxiEntity { Plate = plate.ToUpper() }; _context.Taxis.Add(taxiEntity); await _context.SaveChangesAsync(); } return(Ok(_converterHelper.ToTaxiResponse(taxiEntity))); }
public async Task <IActionResult> GetTaxiEntity([FromRoute] string plaque) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } TaxiEntity taxiEntity = await _context.Taxis .Include(t => t.User) //Driver .Include(t => t.Trips) .ThenInclude(t => t.TripDetails) .Include(t => t.Trips) .ThenInclude(t => t.User) //Passanger .FirstOrDefaultAsync(t => t.Plaque == plaque); if (taxiEntity == null) { return(NotFound()); } return(Ok(_converterHelper.ToTaxiResponse(taxiEntity))); }
public async Task <IActionResult> AddIncident([FromBody] IncidentRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } UserEntity userEntity = await _userHelper.GetUserAsync(request.UserId); if (userEntity == null) { return(BadRequest("User doesn't exists.")); } TaxiEntity taxiEntity = await _context.Taxis.FirstOrDefaultAsync(t => t.Plate == request.Plate); if (taxiEntity == null) { _context.Taxis.Add(new TaxiEntity { Plate = request.Plate.ToUpper() }); await _context.SaveChangesAsync(); taxiEntity = await _context.Taxis.FirstOrDefaultAsync(t => t.Plate == request.Plate); } TripEntity tripEntity = new TripEntity { Source = request.Address, SourceLatitude = request.Latitude, SourceLongitude = request.Longitude, StartDate = DateTime.UtcNow, Taxi = taxiEntity, EndDate = DateTime.UtcNow, Qualification = 1, Remarks = request.Remarks, Target = request.Address, TargetLatitude = request.Latitude, TargetLongitude = request.Longitude, TripDetails = new List <TripDetailEntity> { new TripDetailEntity { Date = DateTime.UtcNow, Latitude = request.Latitude, Longitude = request.Longitude }, new TripDetailEntity { Date = DateTime.UtcNow, Latitude = request.Latitude, Longitude = request.Longitude } }, User = userEntity, }; _context.Trips.Add(tripEntity); await _context.SaveChangesAsync(); return(NoContent()); }
TaxiResponse IConverterHelper.ToTaxiResponse(TaxiEntity taxiEntity) { throw new System.NotImplementedException(); }