public async Task <Blog> UpsertAsync(BlogCreate blogCreate, int applicationUserId) { var datatable = new DataTable(); datatable.Columns.Add("BlogId", typeof(int)); datatable.Columns.Add("Titel", typeof(string)); datatable.Columns.Add("Content", typeof(string)); datatable.Columns.Add("PhotoId", typeof(int)); datatable.Rows.Add(blogCreate.BlogId, blogCreate.Titel, blogCreate.Content, blogCreate.PhotoId); int?newBlogId; using (var connection = new SqlConnection(_config.GetConnectionString("DefaultConnection"))) { await connection.OpenAsync(); newBlogId = await connection.ExecuteScalarAsync <int?>( "Blog_Upsert", new { Blog = datatable.AsTableValuedParameter("dbo.BlogType"), ApplicationUserId = applicationUserId }, commandType : CommandType.StoredProcedure ); } newBlogId = newBlogId ?? blogCreate.BlogId; Blog blog = await GetAsync(newBlogId.Value); return(blog); }
public async Task <bool> UpdateBlogAsync(BlogCreate blogCreate, int applicationUserId) { var blog = new Blog { AppUserId = applicationUserId, Title = blogCreate.Title, Content = blogCreate.Content, // PhotoId = blogCreate.PhotoId, }; _context.Blogs.Update(blog); var updated = await _context.SaveChangesAsync(); return(updated > 0); }
public async Task <bool> CreateBlogAsync(BlogCreate blogCreate, AppUser user) { var blog = new Blog { Title = blogCreate.Title, Content = blogCreate.Content, Username = user.UserName, AppUserId = user.Id, AppUser = user }; _context.Blogs.Add(blog); var created = await _context.SaveChangesAsync(); return(created > 0); }
public async Task <ActionResult <Blog> > Create(BlogCreate blogCreate) { int applicationUserID = int.Parse(User.Claims.First(i => i.Type == JwtRegisteredClaimNames.NameId).Value); if (blogCreate.PhotoID.HasValue) { var photo = await _photoRepository.GetAsync(blogCreate.PhotoID.Value); if (photo.ApplicationUserID != applicationUserID) { return(BadRequest("You did not upload the photo")); } } var blog = await _blogRepository.UpsertAsync(blogCreate, applicationUserID); return(Ok(blog)); }
public async Task <ActionResult <Blog> > Create(BlogCreate blogCreate) { var user = await _unitOfWork.UserRepository.GetUserByUsername(User.GetUsername()); if (blogCreate.PhotoId.HasValue) { var photo = await _unitOfWork.PhotoRepository.GetBlogPhotoAsync(blogCreate.PhotoId.Value); if (photo.AppUserId != user.Id) { return(BadRequest("You did not upload the photo.")); } } var blog = await _unitOfWork.BlogRepository.CreateBlogAsync(blogCreate, user); return(Ok()); }