public async void TestPutBlog() { var now = DateTimeOffset.UtcNow; var blog = new Blog() { Name = "Blog for PutBlog", UserId = this.userId }; var db = this.factory.CreateDbContext(); db.Blogs.Add(blog); db.SaveChanges(); var body = new BlogEditDto() { Name = "Updated Blog" }; var response = await this.authedClient.PutAsJsonAsync($"/api/blogs/{blog.Id}", body); var responseString = await response.Content.ReadAsStringAsync(); Assert.True(response.IsSuccessStatusCode, responseString); var dbblog = this.factory.CreateDbContext().Blogs.Find(blog.Id); Assert.NotNull(dbblog); Assert.Equal(body.Name, dbblog.Name); Assert.True(dbblog.UpdatedAt > now); }
public async Task <GetBlogForEditOutput> GetForEdit(NullableIdDto <Guid> input) { var output = new GetBlogForEditOutput(); BlogEditDto editDto; if (input.Id.HasValue) { var entity = await _blogRepository.GetAllIncluding(x => x.BlogUser).FirstOrDefaultAsync(x => x.Id == input.Id.Value); editDto = ObjectMapper.Map <BlogEditDto>(entity); //var tagsList = await GetTagsOfPost(entity.Id); //if (tagsList.Count > 0) //{ // editDto.TagIds = tagsList.Select(t => t.Id).ToList(); //} } else { editDto = new BlogEditDto(); } output.Blog = editDto; return(output); }
public async void TestPostBlog() { var now = DateTimeOffset.UtcNow; var body = new BlogEditDto() { Name = "New Blog" }; var response = await this.authedClient.PostAsJsonAsync("/api/blogs", body); var responseString = await response.Content.ReadAsStringAsync(); Assert.True(response.IsSuccessStatusCode, responseString); var json = JsonConvert.DeserializeObject <BlogDto>(responseString); Assert.True(json.Id > 0); Assert.Equal(body.Name, json.Name); Assert.True(json.CreatedAt > now); Assert.True(json.UpdatedAt > now); var dbblog = this.factory.CreateDbContext().Blogs.Find(json.Id); Assert.NotNull(dbblog); Assert.Equal(body.Name, dbblog.Name); Assert.Equal(json.CreatedAt, dbblog.CreatedAt); Assert.Equal(json.UpdatedAt, dbblog.UpdatedAt); }
/// <summary> /// ブログを登録する。 /// </summary> /// <param name="userId">ユーザーID。</param> /// <param name="param">ブログ情報。</param> /// <returns>登録したブログ。</returns> public async Task <BlogDto> CreateBlog(int userId, BlogEditDto param) { var blog = this.mapper.Map <Blog>(param); blog.UserId = userId; blog = await this.blogRepository.Create(blog); return(this.mapper.Map <BlogDto>(blog)); }
/// <summary> /// ブログを更新する。 /// </summary> /// <param name="userId">ユーザーID。</param> /// <param name="blogId">ブログID。</param> /// <param name="param">ブログ情報。</param> /// <returns>処理結果。</returns> /// <exception cref="NotFoundException">ブログが存在しない場合。</exception> public async Task UpdateBlog(int userId, int blogId, BlogEditDto param) { var blog = await this.blogRepository.FindOrFail(blogId); if (blog.UserId != userId) { throw new ForbiddenException($"id={blogId} does not belong to me"); } await this.blogRepository.Update(this.mapper.Map(param, blog)); }
protected virtual async Task Update(BlogEditDto input) { var user = await UserManager.GetUserByIdAsync(input.BlogUserId); //TODO:更新前的逻辑判断,是否允许更新 var entity = await _blogRepository.GetAsync(input.Id.Value); entity.SetName(input.Name); entity.SetShortName(input.ShortName); entity.BlogUser = user; //将input属性的值赋值到entity中 ObjectMapper.Map(input, entity); await _blogManager.UpdateAsync(entity); if (input.TagIds?.Count > 0) { await _tagManager.SaveTags(input.TagIds, entity); } }
protected virtual async Task <BlogEditDto> Create(BlogEditDto input) { var user = await UserManager.GetUserByIdAsync(input.BlogUserId); //TODO:新增前的逻辑判断,是否允许新增 var entity = ObjectMapper.Map <Blog>(input); entity.BlogUser = user; // AsyncHelper.RunSync //调用领域服务 entity = await _blogManager.CreateAsync(entity); if (input.TagIds != null && input.TagIds.Count > 0) { await _tagManager.SaveTags(input.TagIds, entity); } var dto = ObjectMapper.Map <BlogEditDto>(entity); return(dto); }
public async Task <ActionResult <BlogDto> > PostBlog(BlogEditDto body) { var blog = await this.blogService.CreateBlog(this.UserId, body); return(this.CreatedAtAction(nameof(this.GetBlog), new { id = blog.Id }, blog)); }
public async Task <IActionResult> PutBlog(int id, BlogEditDto body) { await this.blogService.UpdateBlog(this.UserId, id, body); return(this.NoContent()); }