public async Task CreateFromViewModel(BookEditViewModel model, ApplicationUser user) { var book = new Book { Ebook = model.Ebook, Lent = model.Lent, //Genre = model.Genre, Isbn = model.Isbn, Place = model.Place, ReleaseYear = model.ReleaseYear, Series = model.Series, Seriesnumber = model.Seriesnumber, Subtitel = model.Subtitel, Titel = model.Titel, User = user }; if (model.PublisherName != null) { book.Publisher = await GetOrGeneratePublisher(model.PublisherName); } if (model.AuthorName != null) { var authors = model.AuthorName.Split("; "); foreach (var item in authors) { var author = await _AuthorService.FindByName(item); if (author == null) { author = new Author { Name = item, CreatedOn = DateTime.Now }; author = await _AuthorService.Create(author); } book.BookAuthors.Add(new BookAuthor { BookId = book.Id, AuthorId = author.Id }); } } _AppContext.Books.Add(book); await _AppContext.SaveAsync(); }
// public async Task<IActionResult> Create([FromBody]CreateUserDto userDto) public IActionResult Create([FromBody] CreateUserDto userDto) { var author = _authorService.Create(userDto.Username, userDto.Email, userDto.Password, userDto.FirstName, userDto.MiddleName, userDto.LastName); if (author == null) { return(BadRequest(new ErrorResponseDto { InternalErrorMessage = "Failed to create new user.", DisplayErrorMessage = "Failed to create new user.", })); } var createUserResponseDto = _mapper.Map <CreateUserResponseDto>(author); // try // { // // Notify all subscribers (logged in users) that a new project has been created. // await _userHubContext.Clients // .Groups(Constants.UserSubscriberGroupName) // .SendAsync(Constants.ClientUserNotificationMethodName, new // { // CreatedUserDto = createUserResponseDto, // }); // } // catch (Exception ex) // { // Console.WriteLine($"Encountered excetpion while attempting to publish user creation to subscribers. Message: {ex.Message}"); // Console.WriteLine(ex.StackTrace); // } return(Ok(createUserResponseDto)); }
public async Task <IActionResult> Create([FromBody] AuthorRequestDto authReqDto) { _logger.LogInfo("Creating Author"); try { if (authReqDto == null) { return(BadRequest("Input data is null")); } if (!ModelState.IsValid) { return(BadRequest("Mandatory fields First Name, Last Name missing")); } var authDto = _mapper.Map <AuthorDTO>(authReqDto); var result = await _authorService.Create(_mapper.Map <Author>(authDto)); _logger.LogInfo(result ? $"Author {authReqDto.FirstName} created" : $"Author {authReqDto.FirstName} failed"); return(Created("Created author successfully", new { authReqDto })); } catch (Exception ex) { return(BadRequest( $"Exception Occurred: {ex.Message} {Environment.NewLine} {ex.InnerException} {Environment.NewLine}{ex.StackTrace}")); } }
public async Task <ActionResult <AuthorDTO> > CreateAuthor([FromBody] SaveAuthorDTO saveAuthorResource) { try { var validator = new SaveAuthorResourceValidator(); var validationResult = await validator.ValidateAsync(saveAuthorResource); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); } var authorToCreate = _mapper.Map <SaveAuthorDTO, Author>(saveAuthorResource); var newAuthor = await _authorService.Create(authorToCreate); var author = await _authorService.GetAuthorById(newAuthor.Id); var authorResource = _mapper.Map <Author, AuthorDTO>(author); return(BuildObjectResult(HttpStatusCode.OK, true, value: authorResource)); } catch (Exception ex) { return(BuildObjectResult(HttpStatusCode.BadRequest, false, ex.Message)); } }
public ActionResult Create(AuthorPostModel model) { var author = _mapper.Map <AuthorModel>(model); _authorService.Create(author); return(RedirectToAction("Index")); }
public ActionResult Create(Autor author) { if (_service.Create(author)) { return(RedirectToAction("Index")); } return(View(author)); }
public ActionResult <Author> Post([FromBody] Author author) { if (author == null) { return(BadRequest()); } _authorService.Create(author); return(Ok(author)); }
public async Task <ActionResult> Create(AuthorRequestViewModel author) { if (ModelState.IsValid) { await _authorService.Create(_mapper.Map <AuthorDTO>(author)); } return(RedirectToAction(nameof(CreateUpdate), new { id = author.Id })); }
public ActionResult Create(Author author) { if (ModelState.IsValid) { var products = _authorService.Create(author); return(RedirectToAction("Index")); } return(PartialView(author)); }
public ActionResult Create(AuthorViewModel author) { if (!ModelState.IsValid) { return(View(author)); } var resultMap = _mapper.Map <AuthorModel>(author); _service.Create(resultMap); return(RedirectToAction("Index")); }
public async Task <IActionResult> Post([FromBody] PostAuthorRequestModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok(await Authors.Create( model.FirstName, model.LastName))); }
public async Task <IActionResult> Create(CreateAuthorModel author) { Author model = _mapper.Map <CreateAuthorModel, Author>(author); Author dbObject = await _authorService.Create(model); if (dbObject == null) { return(BadRequest()); } return(Created("Created", _mapper.Map <Author, SuccessAuthorModel>(dbObject))); }
public async Task <IActionResult> Post([FromBody] AuthorViewModel model) { if (!ModelState.IsValid) { return(BadRequest(model)); } var result = await _service.Create(model); return(Created($"{Request.GetDisplayUrl()}/{result.Id}", result)); }
public async Task <ActionResult> Post([FromBody] AuthorDto dto) { try { return(Ok(await AuthorService.Create(dto))); } catch (BusinessRulesException e) { return(StatusCode((int)e.StatusCode, e.Message)); } }
public ActionResult Create(AuthorViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var modelBL = _mapper.Map <AuthorModel>(model); _service.Create(modelBL); return(RedirectToAction("Index")); }
public async Task <ActionResult <AuthorEntity> > Store(AuthorEntity author) { dynamic creating = await _createService.Create(author); if (creating.status == true) { return(Ok(_helpers.SuccessResponse(creating.message, creating.data))); } else { return(StatusCode(500, _helpers.ErrorResponse("Error To create", new string[] { creating.message }))); } }
public ActionResult AddAuthor(AuthorEditViewModel authorEditViewModel) { if (!ModelState.IsValid) { return(View(authorEditViewModel)); } var author = Mapper.Map <Author>(authorEditViewModel); _authorService.Create(author); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create([FromBody] AuthorViewModel author) { try { await _authorService.Create(author); return(Ok()); } catch (Exception e) { return(BadRequest(e.InnerException.Message)); } }
public JsonResult Add(Author auth) { try { auth.Status = 1; _authorService.Create(auth); return(Json(new { success = true, message = "Thêm thành công" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { success = false, message = ex.ToString() }, JsonRequestBehavior.AllowGet)); } }
public async Task TestMethoCreateAsync() { var result = await _authorService.Create(new Author { Id = Guid.NewGuid(), CreatedBy = Guid.NewGuid(), CreatedOn = DateTime.Now, LastName = "asdfasdf", Name = "dafdsf", State = true }); result.Should().Be(default(Author)); }
public ActionResult Create(AuthorModel model) { //TODO: Add insert logic here if (string.IsNullOrEmpty(model.Avatar)) { model.Avatar = "~/Resources/user-blank.png"; } var modelBL = _mapper.Map <AuthorBL>(model); _authorService.Create(modelBL); return(RedirectToAction("Index")); }
public ActionResult Create(AuthorModel author) { try { var authorBL = new AuthorBL { ArticleId = author.ArticleId, FirstName = author.FirstName, LastName = author.LastName, NickName = author.NickName }; authorService.Create(authorBL); return(Content("<h2>Completed</h2>")); } catch (ValidationException ex) { ModelState.AddModelError(ex.Property, ex.Message); } return(View(author)); }
public async Task <IActionResult> Create(Author author) { try { if (author == null) { return(BadRequest("Author fail")); } var newAuthor = await _authorService.Create(author); return(Ok(newAuthor)); } catch (Exception ex) { return(Problem(ex.Message)); } }
public IActionResult Register([FromBody] GetAuthorDto authorDto) { // map dto to entity var author = _mapper.Map <Author>(authorDto); try { // save _authorService.Create(author, authorDto.Password); return(Ok()); } catch (AppException ex) { // return error message if there was an exception return(BadRequest(ex.Message)); } }
public IActionResult Create([FromBody] CreateAuthorViewModel author) { try { _authorService.Create(author); return(Ok(author)); } catch (BusinessLogicException exception) { return(BadRequest(exception.Message)); } catch (Exception exception) { _logger.LogInformation(exception.Message); return(StatusCode((int)HttpStatusCode.InternalServerError)); } }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,DateOfBirth")] Author author) { if (!ModelState.IsValid) { return(View(author)); } var sameAuthor = await _authorService.Get(author.FirstName, author.LastName, author.DateOfBirth); if (sameAuthor != null) { ModelState.AddModelError("", $"{author.FirstName} {author.LastName} born {author.DateOfBirth:dd/MM/yyy} already exists"); return(View(author)); } await _authorService.Create(author); return(RedirectToAction(nameof(Index))); }
public IActionResult Create([Bind("Id,FirstName,LastName,Biography")] AuthorViewModel author, IFormFile file) { if (ModelState.IsValid) { try { var newAuthor = _mapper.Map <AuthorViewModel, AuthorDto>(author); _service.Create(newAuthor, file); } catch (Exception e) { return(View("Error", new ErrorViewModel { RequestId = Request.HttpContext.TraceIdentifier, Exception = e })); } return(RedirectToAction(nameof(Index))); } return(View(author)); }
public void Can_Add_Author() { //Arrange int Id = 1; Author author = new Author { FullName = "Femi Adesina" }; _mockRepository.Setup(m => m.Add(author)).Returns((Author a) => { a.Id = Id; return(a); }); //Act _service.Create(author); //Assert Assert.AreEqual(Id, author.Id); //Verify that Repository was called only once _mockRepository.Verify(m => m.Add(author), Times.Once); }
public async Task <IActionResult> Create([FromBody] AuthorViewModel authorVM) { var created = await _authorService.Create(authorVM.MapTo <Author>()); return(created ? Ok() as ActionResult : BadRequest()); }
// POST: api/Author public void Post(AuthorViewModel author) { var model = Mapper.Map <AuthorModel>(author); authorService.Create(model); }