コード例 #1
0
        public async Task <IActionResult> AddModel(AddModelDto newModel)
        {
            ServiceResponse <Model> response = await _modelService.AddModel(newModel);

            if (response.Data == null)
            {
                return(NotFound(response));
            }
            return(Ok(response));
        }
コード例 #2
0
        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();
        }
コード例 #3
0
 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));
     }
 }
コード例 #4
0
        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);
        }
コード例 #5
0
 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());
 }