Example #1
0
        public async Task <APIResponse <Model> > Post([FromBody] ModelDto modelDto)
        {
            // Mapping dto with model
            var model = _mapper.Map <Model>(modelDto);

            // Valdate model
            if (!ModelState.IsValid)
            {
                var errors = new List <string>();
                foreach (var state in ModelState)
                {
                    foreach (var error in state.Value.Errors)
                    {
                        errors.Add(error.ErrorMessage);
                    }
                }

                var err = new { errors = errors };

                return(new APIResponse <Model>(400, err, null));
            }

            var modelAdded = await _modelService.AddModelAsync(model);

            if (modelAdded != null)
            {
                return(new APIResponse <Model>(201, "Model is created", modelAdded));
            }
            return(new APIResponse <Model>(204, "Model is not created", modelAdded));
        }
Example #2
0
        public async Task <ActionResult <ModelDto> > Post(int accountId, [FromBody] ModelDto newModel)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var result = await _modelService.AddModelAsync(accountId, newModel);

                return(Ok(result));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(BadRequest("Error adding model"));
            }
        }
Example #3
0
        public async Task <IActionResult> AddModel([FromForm] ModelDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Something want wrong while adding model"));
            }

            var modelToAdd = new Model
            {
                BrandId = model.BrandId,
                Name    = model.Name
            };

            await _modelService.AddModelAsync(modelToAdd);

            await _genericRepository.SaveChangesAsync();

            return(Ok(new
            {
                status = 200,
                message = "Model Added successfully!"
            }));
        }
Example #4
0
        public async Task <IActionResult> Add(Model model)
        {
            await _modelService.AddModelAsync(model); //Добавление модели в БД

            return(Redirect("~/Home/Index"));         //после добавления происходит переход на главную страницу
        }