public async Task <IActionResult> PutPost(int id, Post post) { if (id != post.Id) { return(BadRequest()); } _context.Entry(post).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutPost([FromRoute] Guid id, [FromBody] Post post) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != post.MessageId) { return(BadRequest()); } _context.Entry(post).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
/// <summary> /// Delete post by id /// </summary> /// <param name="id">int</param> /// <returns>Task</returns> public async Task Delete(int id) { Post post = await _context.Posts.FindAsync(id); _context.Posts.Remove(post); await _context.SaveChangesAsync(); }
public async Task DeleleAsync(int id) { Post post = await _context.Posts.FindAsync(id); if (post != null) { _context.Posts.Remove(post); await _context.SaveChangesAsync(); } }
/// <summary> /// After receiving a Post object from the form data on a page, it attempts to find a matching primary key in the database. If no match is found the given Post object is added to the table. If a match is found then the properties of the exsisting object are updated with the data from the form. /// </summary> /// <param name="Post">Post object created from form data</param> /// <returns>New or updated Post object</returns> public async Task SaveAsync(Post Post) { if (await _context.Posts.FirstOrDefaultAsync(m => m.ID == Post.ID) == null) { _context.Posts.Add(Post); } else { _context.Posts.Update(Post); } await _context.SaveChangesAsync(); }
/// <summary> /// Delete a post /// </summary> /// <param name="id">post id</param> /// <returns>home page</returns> public async Task DeleteAsync(int id) { //check to see if the post exists in the db Post post = await _context.Posts.FindAsync(id); if (post != null) { _context.Remove(post); //remove and save the changes await _context.SaveChangesAsync(); } }
public async Task <IActionResult> AddPost([FromBody] PostInputModel inputModel) { try { var category = context.PostCategories.SingleOrDefault(c => c.CategoryID == inputModel.CategoryID); if (category == null) { return(BadRequest("Invalid category Id")); } context.Posts.Add(new Post { Title = inputModel.Title, Content = inputModel.Content, CategoryID = inputModel.CategoryID, PostedDate = DateTime.UtcNow, LastUpdatedDate = DateTime.UtcNow, UserID = HttpContext.Session.GetInt32("USER_LOGIN_KEY").Value }); await context.SaveChangesAsync(); return(Ok()); } catch (DbUpdateException e) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task <Estadio> UpdateEstadio(Estadio estadio) { try { context.Estadio.Update(estadio); await context.SaveChangesAsync(); } catch (Exception e) { throw new NotImplementedException(); } return(estadio); }
public async Task <Hincha> CreateHincha(Hincha hincha) { try { context.Hincha.Add(hincha); await context.SaveChangesAsync(); } catch (Exception e) { throw new NotImplementedException(); } return(hincha); }
public async Task <IActionResult> Create([Bind("id,author,date,message,ImageFile")] Post post) { if (ModelState.IsValid) { if (post.ImageFile != null) { using (var memoryStream = new MemoryStream()) { post.ImageFile.CopyTo(memoryStream); var byteArray = memoryStream.ToArray(); post.image = Convert.ToBase64String(byteArray); } } post.date = DateTime.Now; _context.Add(post); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(post)); }
public async Task <ControlEntrada> CreateControlEntrada(ControlEntrada ControlEntrada) { try { context.ControlEntrada.Add(ControlEntrada); await context.SaveChangesAsync(); } catch (Exception e) { throw new NotImplementedException(); } return(ControlEntrada); }
public async Task <Post> SubmitPost([Service] PostDbContext dbContext, SubmitPostInput input) { var post = new Post { Header = input.Header, Content = input.Content, CreatedAt = DateTime.Now, CreatedBy = input.Author }; await dbContext.Posts.AddAsync(post); await dbContext.SaveChangesAsync(); return(post); }
public static async Task Seed(PostDbContext dbContext) { // thangchung's user blog var blogId = new BlogId(IdHelper.GenerateId("34c96712-2cdf-4e79-9e2f-768cb68dd552")); var authorId = new AuthorId(IdHelper.GenerateId("4b5f26ce-df97-494c-b747-121d215847d8")); for (var i = 1; i <= 100; i++) { var post = Core.Domain.Post.CreateInstance( blogId, $"The title of post {i}", $"The excerpt of post {i}", $"The body of post {i}", authorId) .AddComment("comment 1", authorId) .AssignTag($"{i}"); dbContext.Add(post); } await dbContext.SaveChangesAsync(); }
public async Task Insert(T entity) { _entities.Add(entity); await _context.SaveChangesAsync(); }
public async Task SaveChangesAsync() { await _context.SaveChangesAsync(); }
/// <summary> /// Adds a new user comment to the Comment table relative to the post it was intended for. /// </summary> /// <param name="comment">New Comment object</param> /// <returns>New Comment object</returns> public async Task SaveAsync(Comment comment) { await _context.Comments.AddAsync(comment); await _context.SaveChangesAsync(); }