public void Save(Comment.Comment comment) { using (_context) { using (var dbContextTransaction = _context.Database.BeginTransaction()) { try { var model = comment.ToModel(); if (comment.New) { _comments.Add(model); } else { _context.Entry(model).State = EntityState.Modified; } _context.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); } } } }
public async Task CreateTicket_ReturnsOk_WhenPost() { // Arrange var controller = new CommentsController(); var comment = new Comment.Comment() { UserId = -1, TicketId = -1, Message = "Test", PostedDate = DateTime.Now, AddDate = DateTime.Now, UpdateDate = DateTime.Now, User = new User.User() { UserId = -1, UserName = "******", } }; // Act var response = controller.CreateTicket(comment); // Assert var result = Assert.IsType <OkObjectResult>(response.Result); Assert.Equal(200, result.StatusCode); }
public static CommentModel ToModel(this Comment.Comment comment) { return(new CommentModel { Id = comment.Id, Text = comment.Text, Approved = comment.Approved, Deleted = comment.Deleted }); }
public static Comment.Comment ToDomain(this CommentModel model) { var comment = new Comment.Comment(); comment.GetType().GetProperty("PostId").SetValue(comment, model.PostId, null); comment.GetType().GetProperty("Id").SetValue(comment, model.Id, null); comment.GetType().GetProperty("Text").SetValue(comment, model.Text, null); comment.GetType().GetProperty("Approved").SetValue(comment, model.Approved, null); return(comment); }
public virtual async System.Threading.Tasks.Task Comment(Guid memberId, string content, IMembershipService authorizationService) { await authorizationService.CheckUserMembership(memberId, ProjectId); var comment = new Comment.Comment(memberId, content); if (Comments == null) { Comments = new List <Comment.Comment>(); } Comments.Add(comment); }
public static Comment.Comment Add(pillar.Comment.Comment comment) { try { using (var conn = new NpgsqlConnection(connString)) { conn.Open(); using (var cmd = new NpgsqlCommand()) { cmd.Connection = conn; cmd.CommandText = "INSERT INTO comments (ticketid, userid, message, posteddate, added, updated)" + "VALUES (@ticketid, @userid, @message, @posteddate, @added, @updated);" + "select currval('comments_id_seq');"; cmd.Parameters.AddWithValue("ticketid", comment.TicketId); cmd.Parameters.AddWithValue("userid", comment.UserId); cmd.Parameters.AddWithValue("message", comment.Message); cmd.Parameters.AddWithValue("posteddate", comment.PostedDate); cmd.Parameters.AddWithValue("added", comment.AddDate); cmd.Parameters.AddWithValue("updated", comment.UpdateDate); var reader = cmd.ExecuteReader(); int commentId; while (reader.Read()) { commentId = reader.GetInt32(0); var currentComment = new Comment.Comment() { UserId = comment.UserId, TicketId = comment.TicketId, Message = comment.Message, User = new User.User() { UserName = comment.User.UserName, Requester = comment.User.Requester, Title = comment.User.Title, Organization = comment.User.Organization }, PostedDate = comment.PostedDate, AddDate = comment.AddDate, UpdateDate = comment.UpdateDate, }; Console.WriteLine($"Comment #{commentId} added to {comment.TicketId} on {comment.PostedDate}."); conn.Close(); var json = JsonConvert.SerializeObject(comment); Console.WriteLine(json); return(currentComment); } conn.Close(); return(null); } } } catch (NpgsqlException err) { Console.Write(err); return(null); } }