public async Task <Result> Create(NewNovelDto data) { var result = new Result(); // validate required fields if (string.IsNullOrWhiteSpace(data.Author)) { result.Message = "Author is required"; } else if (string.IsNullOrWhiteSpace(data.Description)) { result.Message = "Description is required"; } else if (string.IsNullOrWhiteSpace(data.Title)) { result.Message = "Title is required"; } else { var novel = _mapper.Map <Novel>(data); result = await _dbContext.Novel.Insert(novel); if (result.IsSuccess) { result.Message = $"Novel '{data.Title}' added!"; } } return(result); }
public async Task CreateNewNovel_MissingRequiredData_ShouldReturnFalse() { var novel = new NewNovelDto { Author = "", Description = "", Title = "CreateNewNovel_WrongInput_ShouldReturnFalse", }; var result = await _novelService.Create(novel); Assert.IsFalse(result.IsSuccess); }
public async Task CreateNewNovel_ShouldReturnTrue() { var novel = new NewNovelDto { Author = "CreateNewNovel_ShouldInsertToMockData", Description = "CreateNewNovel_ShouldInsertToMockData", Title = "CreateNewNovel_ShouldInsertToMockData", }; var result = await _novelService.Create(novel); var all = await _novelService.All(); var novelFromMockDb = all.FirstOrDefault(m => m.Title == "CreateNewNovel_ShouldInsertToMockData"); Assert.IsTrue(result.IsSuccess); Assert.IsNotNull(novelFromMockDb); }
public async Task <IActionResult> Post([FromBody] NewNovelDto data) { var result = await _novelService.Create(data); return(new JsonResult(result)); }