/// <summary> /// Creates new vehicle /// </summary> /// <param name="vehicleViewModel"></param> /// <param name="userId"></param> /// <param name="ct"></param> /// <returns></returns> public async Task <VehicleViewModel> AddVehicleAsync(VehicleRequestViewModel vehicleViewModel, string userId, CancellationToken ct = default(CancellationToken)) { var vehicle = new Vehicle { Plate = vehicleViewModel.Plate, UserId = int.Parse(userId), CreatedAt = DateTime.Now }; vehicle = await _repository.AddAsync(vehicle, ct); return(VehicleConverter.Convert(vehicle)); }
/// <summary> /// Updates a vehicle /// </summary> /// <param name="id"></param> /// <param name="vehicleViewModel"></param> /// <param name="userId"></param> /// <param name="ct"></param> /// <returns></returns> public async Task <bool> UpdateVehicleAsync(int id, VehicleRequestViewModel vehicleViewModel, int userId, CancellationToken ct = default(CancellationToken)) { var vehicle = await _repository.GetByIdAsync(id, ct); if (vehicle == null) { return(false); } vehicle.Plate = vehicleViewModel.Plate; vehicle.UserId = userId; vehicle.UpdatedAt = DateTime.Now; return(await _repository.UpdateAsync(vehicle, ct)); }
public async Task <IActionResult> Post([FromBody] VehicleRequestViewModel input, CancellationToken ct = default(CancellationToken)) { try { if (input == null) { return(BadRequest()); } var userId = ((ClaimsIdentity)User.Identity).Name; return(StatusCode(201, await _manager.AddVehicleAsync(input, userId, ct))); } catch (Exception ex) { return(StatusCode(500, new { Code = "ServerError", ex.Message })); } }
public async Task <IActionResult> Put(int id, [FromBody] VehicleRequestViewModel input, CancellationToken ct = default(CancellationToken)) { try { if (input == null) { return(BadRequest()); } var userId = int.Parse(User.Identity.Name); if (!await _manager.isValidUserVehicle(userId, id, ct)) { return(Forbid()); } if (await _manager.GetVehicleByIdAsync(id, ct) == null) { return(NotFound()); } if (await _manager.UpdateVehicleAsync(id, input, userId, ct)) { return(Ok(input)); } return(StatusCode(500)); } catch (Exception ex) { return(StatusCode(500, new { Code = "ServerError", ex.Message })); } }