private static void DisplayStoryCreatedSummary(StoryDto newStory) { Console.WriteLine("Created story with id {0} and description '{1}' in project named {2}", newStory.V1StoryId, newStory.GetAttribute("Description"), newStory.V1ProjectName); }
public async Task <StoryDto> Update(StoryDto story) { var storyAsEntity = this.mapper.Map <StoryEntity>(story); var result = await this.storyRepository.Update(storyAsEntity); return(mapper.Map <StoryDto>(result)); }
public async Task <IActionResult> Create([FromBody] StoryDto story) { this._logger.LogDebug($"POST api/story"); await this._storyService.Create(story); return(this.Created("api/stories", story)); }
public async Task <StoryDto> Create(StoryDto story) { var storyAsEntity = mapper.Map <StoryEntity>(story); var createdEntity = await this.storyRepository.Create(storyAsEntity); var result = mapper.Map <StoryDto>(createdEntity); return(result); }
public ActionResult Put(int id, [FromForm] StoryInsertDto story) { story.Id = id; try { string newFileName = null; if (story.Picture != null) { var extension = Path.GetExtension(story.Picture.FileName); if (!FileUpload.AllowedExtensions.Contains(extension)) { return(UnprocessableEntity("You must upload image.")); } newFileName = Guid.NewGuid().ToString() + "_" + story.Picture.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName); story.Picture.CopyTo(new FileStream(filePath, FileMode.Create)); } List <JournalistDto> journalists = new List <JournalistDto>(); foreach (var journalistId in story.Journalists) { journalists.Add(this.getJournalistCommand.Execute(journalistId)); } StoryDto storyDto = new StoryDto { Id = story.Id, IsActive = story.IsActive, Name = story.Name, Description = story.Description, PicturePath = newFileName, CategoryId = story.CategoryId, Journalists = journalists }; this.updateStoryCommand.Execute(storyDto); return(NoContent()); } catch (EntityNotFoundException) { return(NotFound()); } catch (EntityAlreadyExistsException) { return(Conflict()); } catch (Exception) { return(StatusCode(500)); } }
public void TestDtoMappingWithNull() { var dto = new StoryDto() { By = "test", Id = 122, Score = 1, Time = 1570887781, Descendants = 123, Title = "test", Type = "Post", Url = "test url", Kids = null }; var story = StoryDto.ConvertToStory(dto); Assert.AreEqual(story.CommentCount, 0); }
public ActionResult Edit(int id, [FromForm] StoryInsertDto story) { story.Id = id; if (!ModelState.IsValid) { return(View()); } try { string newFileName = null; if (story.Picture != null) { var extension = Path.GetExtension(story.Picture.FileName); if (!FileUpload.AllowedExtensions.Contains(extension)) { return(UnprocessableEntity("You must upload image.")); } newFileName = Guid.NewGuid().ToString() + "_" + story.Picture.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName); story.Picture.CopyTo(new FileStream(filePath, FileMode.Create)); } List <JournalistDto> journalists = new List <JournalistDto>(); foreach (var journalistId in story.Journalists) { journalists.Add(this.getJournalistCommand.Execute(journalistId)); } StoryDto storyDto = new StoryDto { Id = story.Id, IsActive = story.IsActive, Name = story.Name, Description = story.Description, PicturePath = newFileName, CategoryId = story.CategoryId, Journalists = journalists }; this.updateStoryCommand.Execute(storyDto); } catch (Exception e) { string asd = e.Message; TempData["error"] = "An error has occured."; } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> CreateNewStory(StoryDto storyDto) { storyDto.StatusId = 1; var storyToSave = _mapper.Map <Story>(storyDto); _context.Stories.Add(storyToSave); await _context.SaveChangesAsync(); int sid = storyToSave.Id; return(CreatedAtRoute("GetNewlyCreatedStory", new { id = storyToSave.Id }, storyToSave)); }
public Task <StoryDto> Add(StoryDto model) { var story = new Story { CreateDate = DateTime.UtcNow, Description = model?.Description, Location = Mapper.Map <Location>(model?.Location) }; return(this.storyService .Add(story) .ContinueWith(x => Mapper.Map <StoryDto>(x.Result))); }
public void Update(StoryDto updatedStory) { Guard.WhenArgument(updatedStory, "updatedStory").IsNull().Throw(); var story = this.storiesRepo.GetById(updatedStory.Id); if (story != null) { story.Title = updatedStory.Title; story.Content = updatedStory.Content; this.storiesRepo.Update(story); } }
public void TestDtoMapping() { var kids = new List <int>() { 2, 3, 7 }; var dto = new StoryDto() { By = "test", Id = 122, Score = 1, Time = 1570887781, Descendants = 123, Title = "test", Type = "Post", Url = "test url", Kids = kids }; var story = StoryDto.ConvertToStory(dto); Assert.AreEqual(story.PostedBy, dto.By); }
public void Can_create_a_single_story_and_get_back_the_v1_id() { const string name = "My first story"; const string description = "Adding stories programmatically to VersionOne is easy..."; var storyDto = new StoryDto("10", name, description); var newStory = _storyManager.Create(storyDto); DisplayStoryCreatedSummary(newStory); Assert.AreNotEqual(0, newStory.V1StoryId); Assert.AreEqual(name, newStory.Name); Assert.AreEqual(description, newStory.GetAttribute("Description")); }
public async Task <Story> GetSingleStorie(int id) { var url = $"https://hacker-news.firebaseio.com/v0/item/{id}.json"; HttpResponseMessage response = await _client.GetAsync(url); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); var dto = JsonConvert.DeserializeObject <StoryDto>(json); var story = StoryDto.ConvertToStory(dto); return(story); } return(new Story()); }
public async Task <IActionResult> AddStory(Story story) { StoryDto storyDto = new StoryDto(); var result = await storyRepository.AddStoryAsync(story); if (result) { storyDto.Status = EntityStatus.Suceess; } else { storyDto.Status = EntityStatus.Fail; storyDto.Message = "添加故事失败,受影响的行数小于0"; } return(Ok(storyDto)); }
public async Task <IActionResult> DeleteStory(int storyId) { StoryDto storyDto = new StoryDto(); var result = await storyRepository.DeleteStoryAsync(storyId); if (result) { storyDto.Status = EntityStatus.Suceess; } else { storyDto.Status = EntityStatus.Fail; storyDto.Message = $"删除故事出错,故事Id{storyId},受影响的行数小于等于0"; } return(Ok(storyDto)); }
public ActionResult Create(StoryCreateViewModel story) { if (!this.ModelState.IsValid) { return(View(story)); } var storyDto = new StoryDto() { Title = story.Title, Content = story.Content }; var storyId = this.storyService.Create(storyDto, this.User.Identity.GetUserId()); return(this.RedirectToAction("Details", new { id = storyId })); }
public async Task <IActionResult> GetAlllStory(int bookId) { StoryDto storyDto = new StoryDto(); var stories = await storyRepository.GetAllStoryAsync(bookId); if (stories != null && stories.Count > 0) { storyDto.Status = EntityStatus.Suceess; storyDto.Stories = stories; } else { var ex = new SkyWalkerException($"获取所有故事出错bookId:{bookId}"); throw ex; } return(Ok(stories)); }
public async Task GetStoryByIdTest() { IEnumerable <int> storyIds = await _hackerNewsAPI.GetNewStoriesAsync(); Random random = new Random(); int storyPosition = random.Next(0, storyIds.Count()); StoryDto story = await _hackerNewsAPI.GetStoryByIdAsync(storyIds.ToArray()[storyPosition]); Assert.True(story != null, "Story not found"); Assert.True(!string.IsNullOrWhiteSpace(story.Url), "Url not found"); Assert.True(!string.IsNullOrWhiteSpace(story.By), "Author not found"); Assert.True(!string.IsNullOrWhiteSpace(story.Title), "Title not found"); }
public async Task <IActionResult> EditStory(string id, StoryDto storyPatch) { int storyId = Convert.ToInt32(id); Story story = await _context.Stories.FindAsync(storyId); if (story == null) { return(NotFound()); } story.StoryName = storyPatch.StoryName; story.StoryDescription = storyPatch.StoryDescription; story.AcceptanceCriteria = storyPatch.AcceptanceCriteria; story.EpicId = storyPatch.EpicId; story.PriorityId = storyPatch.PriorityId; await _context.SaveChangesAsync(); return(Ok(storyPatch)); }
public Guid Create(StoryDto newStory, string userId) { Guard.WhenArgument(newStory, "newStory").IsNull().Throw(); Guard.WhenArgument(userId, "userId").IsNullOrEmpty().Throw(); var story = new Story() { Id = Guid.NewGuid(), Title = newStory.Title, Content = newStory.Content, MainImageUrl = Constants.DefaultStoryImageUrl, PublishDate = DateTime.Now, UserId = userId }; this.storiesRepo.Add(story); return(story.Id); }
public ActionResult Edit(StoryCreateViewModel story) { if (!this.ModelState.IsValid) { return(View(story)); } var storyDto = new StoryDto() { Id = story.Id, Title = story.Title, Content = story.Content }; this.storyService.Update(storyDto); return(this.RedirectToAction("Details", new { id = story.Id })); }
public async Task <IActionResult> GetStory(int storyId) { var story = await storyRepository.GetStoryAsync(storyId); StoryDto storyDto = new StoryDto(); if (story != null) { storyDto.Status = EntityStatus.Suceess; storyDto.Stories = new List <Story> { story }; } else { var ex = new SkyWalkerException($"获取故事出错storyId:{storyId}"); } return(Ok(story)); }
public async Task <IActionResult> Update([FromBody] StoryDto story, int id) { if (story == null) { return(this.BadRequest()); } this._logger.LogDebug($"PUT api/story/{id}"); story.Id = id; var result = await this._storyService.Update(story); if (result == null) { return(this.NotFound()); } return(this.Ok(result)); }
public void NotCallStoryRepoUpdateOnce_WhenStoryIsExistent() { //Arrange var storiesRepoMock = new Mock <IEfRepository <Story> >(); var storyDto = new StoryDto() { Id = Guid.NewGuid() }; var storyStarsRepoMock = new Mock <IEfRepository <StoryStar> >(); var storyService = new Services.StoryService(storiesRepoMock.Object, storyStarsRepoMock.Object); Story storyFromRepo = null; storiesRepoMock.Setup(m => m.GetById(storyDto.Id)).Returns(storyFromRepo); //Act storyService.Update(storyDto); //Assert storiesRepoMock.Verify(m => m.Update(storyFromRepo), Times.Never); }
public async Task <IActionResult> UpdateStory([FromBody] JsonPatchDocument <Story> patch, int storyId) { StoryDto storyDto = new StoryDto(); //获取故事数据 var story = await storyRepository.GetStoryAsync(storyId); patch.ApplyTo(story); var result = await storyRepository.UpdateStoryAsync(story); if (result) { storyDto.Status = EntityStatus.Suceess; storyDto.Stories = new List <Story> { story }; } else { storyDto.Status = EntityStatus.Fail; storyDto.Message = $"更新故事失败,故事Id:{storyId},受影响的行数小于0"; } return(Ok(storyDto)); }
public void Execute(StoryDto request) { if (!this.Context.Categories.Any(c => c.Id == request.CategoryId)) { throw new EntityNotFoundException("category"); } var story = new Story { Name = request.Name, Description = request.Description, IsActive = request.IsActive, CategoryId = request.CategoryId, PicturePath = request.PicturePath }; this.Context.Stories.Add(story); this.Context.SaveChanges(); foreach (var item in request.Journalists) { if (!this.Context.Journalists.Any(j => j.Id == item.Id)) { throw new EntityNotFoundException("journalist"); } this.Context.StoryJournalist.Add(new StoryJournalist { JournalistId = item.Id, StoryId = story.Id }); } this.Context.SaveChanges(); }