public async Task <IActionResult> AddModel(AddModelDto newModel) { ServiceResponse <Model> response = await _modelService.AddModel(newModel); if (response.Data == null) { return(NotFound(response)); } return(Ok(response)); }
public void Execute(AddModelDto request) { if (Context.Models.Any(m => m.Name == request.Name)) { throw new EntityAlreadyExistsException(); } Context.Models.Add(new Model { ManufacturerId = request.ManufacturerId, Name = request.Name }); Context.SaveChanges(); }
public IActionResult Post([FromBody] AddModelDto dto) { try { _addModelCommand.Execute(dto); return(StatusCode(201, "Successfully added model.")); } catch (EntityAlreadyExistsException) { return(StatusCode(422, "An error has occured.")); } catch (Exception e) { return(StatusCode(500, e)); } }
public async Task <ServiceResponse <Model> > AddModel(AddModelDto newModel) { ServiceResponse <Model> serviceResponse = new ServiceResponse <Model>(); try { var entity = _mapper.Map <Model>(newModel); _context.Models.Add(entity); _context.SaveChanges(); serviceResponse.Data = entity; } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; } return(serviceResponse); }
public ActionResult Create(AddModelDto dto) { if (!ModelState.IsValid) { TempData["error"] = "Oopss, somethng went wrong."; RedirectToAction(nameof(Index)); } try { // TODO: Add insert logic here _addModel.Execute(dto); return(RedirectToAction(nameof(Index))); } catch (EntityAlreadyExistsException) { TempData["error"] = "Model with that name already exists."; } catch (Exception) { TempData["error"] = "An error has occurred."; } return(View()); }