public async Task <ActionResult <VehicleTypeDto> > AddVehicleType([FromBody] VehicleTypeForCreationDto vehicleTypeForCreation) { var validationResults = new VehicleTypeForCreationDtoValidator().Validate(vehicleTypeForCreation); validationResults.AddToModelState(ModelState, null); if (!ModelState.IsValid) { return(BadRequest(new ValidationProblemDetails(ModelState))); //return ValidationProblem(); } var vehicleType = _mapper.Map <VehicleType>(vehicleTypeForCreation); await _vehicleTypeRepo.AddVehicleType(vehicleType); var saveSucessful = await _vehicleTypeRepo.SaveAsync(); if (saveSucessful) { var vehicleTypeFromRepo = await _vehicleTypeRepo.GetVehicleTypeAsync(vehicleType.Id); var vehicleTypeDto = _mapper.Map <VehicleTypeDto>(vehicleTypeFromRepo); var response = new Response <VehicleTypeDto>(vehicleTypeDto); return(CreatedAtRoute("GetVehicleType", new { vehicleType.Id }, response)); } return(StatusCode(500)); }
public int AddVehicleType(VehicleTypeDTO dto) { int id = 0; try { log.Debug(VehicleTypeDTO.FormatVehicleTypeDTO(dto)); R_VehicleType t = VehicleTypeDTO.ConvertDTOtoEntity(dto); // add id = Repository.AddVehicleType(t); dto.VehicleTypeId = id; log.Debug("result: 'success', id: " + id); } catch (System.Exception e) { // error log.Error(e.ToString()); throw; } return(id); }
public async Task <IActionResult> Create([FromForm] VehicleTypeViewModel vehicleTypeVM) { if (ModelState.IsValid) { var newVehicleType = await _vehicleTypeRepo.AddVehicleType(vehicleTypeVM.VehicleType); TempData["message"] = "Your data has been submitted successfully."; TempData["toasterType"] = ToasterType.success; //return RedirectToAction(nameof(Index)); return(RedirectToAction(nameof(Edit), new { id = newVehicleType.Id, returnUrl = vehicleTypeVM.ReturnUrl })); } TempData["message"] = "A problem has been ocurred while submitting your data."; TempData["toasterType"] = ToasterType.info; return(View("Edit", VehicleTypeViewModelFactory.Create(vehicleTypeVM.VehicleType, vehicleTypeVM.ReturnUrl))); }