public IActionResult UpdateTruckById([FromBody] UpdateTruckViewModel updateTruckViewModel) { try { _itruckServices.UpdateTruckViewModel(updateTruckViewModel); } catch (Exception e) { Console.WriteLine(e); throw; } return(Ok()); }
public async Task <IActionResult> UpdateTruck(int id, [FromBody] UpdateTruckViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userId = _caller.Claims.Single(c => c.Type == "id"); var customer = await _context.Customers.Include(c => c.Identity).SingleAsync(c => c.Identity.Id == userId.Value); var truck = _context.Trucks.Find(id); if (truck == null) { return(NotFound()); } if (truck.Created_by != customer) { return(StatusCode(401, "Update not allowed on this truck")); } var old_hours = truck.Hours; var old_location = truck.Location; String hours = model.Hours; Address location = model.Location; truck.Hours = hours; truck.Location = location; _context.SaveChanges(); var audit = new Audit { TruckId = truck.TruckId, TypeOfOperation = "UPDATE", Timestamp = DateTime.Now, Hours = old_hours, Street = old_location.Street, City = old_location.City, State = old_location.State, Country = old_location.Country, Zip = old_location.Zip }; _context.Audits.Add(audit); await _context.SaveChangesAsync(); return(Ok()); }
public void UpdateTruckViewModel(UpdateTruckViewModel updateTruckViewModel) { try { var update = _contextDb.Truck.FirstOrDefault(s => s.Id == updateTruckViewModel.Id); if (update == null) { throw new Exception("Id not found"); } update.UpdateTruck( updateTruckViewModel.Chassis, updateTruckViewModel.TruckModelId, updateTruckViewModel.ManufactureYear, updateTruckViewModel.ResponsableId, updateTruckViewModel.Status); _contextDb.Update(update); _contextDb.SaveChanges(); } catch (Exception e) { throw new Exception(e.Message); } }