public async Task <ActionResult <OfficeDto> > AddOffice([FromBody] OfficeForCreationDto officeForCreation) { var validationResults = new OfficeForCreationDtoValidator().Validate(officeForCreation); validationResults.AddToModelState(ModelState, null); if (!ModelState.IsValid) { return(BadRequest(new ValidationProblemDetails(ModelState))); //return ValidationProblem(); } var office = _mapper.Map <Office>(officeForCreation); await _officeRepository.AddOffice(office); var saveSuccessful = await _officeRepository.SaveAsync(); if (saveSuccessful) { var officeFromRepo = await _officeRepository.GetOfficeAsync(office.Id); var officeDto = _mapper.Map <OfficeDto>(officeFromRepo); var response = new Response <OfficeDto>(officeDto); return(CreatedAtRoute("GetOffice", new { officeDto.Id }, response)); } return(StatusCode(500)); }
public async Task <ResponseViewModel> AddOffice(OfficeViewModel model) { var office = new Office() { Name = model.Name, Adress = model.Adress }; if (office != null) { _officeRepository.AddOffice(office); return(new ResponseViewModel() { Result = true, Message = "Office add!" }); } else { return(new ResponseViewModel() { Result = false, Message = "Office not add!" }); } }
public async Task <IActionResult> Create([FromForm] OfficeViewModel officesVM) { if (ModelState.IsValid) { var newOffice = await _officeRepo.AddOffice(officesVM.Office); TempData["message"] = "Your data has been submitted successfully."; TempData["toasterType"] = ToasterType.success; //return RedirectToAction(nameof(Index)); return(RedirectToAction(nameof(Edit), new { id = newOffice.Id, returnUrl = officesVM.ReturnUrl })); } TempData["message"] = "A problem has been ocurred while submitting your data."; TempData["toasterType"] = ToasterType.info; return(View("Edit", OfficeViewModelFactory.Create(officesVM.Office, officesVM.ReturnUrl))); }
public IActionResult CreateOffice([FromBody] Office office) { if (office == null) { return(BadRequest()); } if (office.Name == string.Empty) { ModelState.AddModelError("Name", "The name of the Office shouldn't be empty"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var createdOffice = _officeRepository.AddOffice(office); return(Created("office", office)); }
public OfficeDto AddOffice(OfficeDto officeDto) { var office = Mapper.Map <OfficeDto, Office>(officeDto); return(Mapper.Map <Office, OfficeDto>(_officeRepository.AddOffice(office))); }