コード例 #1
0
        public static void DeleteResponse(int messageId, string userId, out StringBuilder errors)//Can be used for deleting a response or marking a response as read.
        {
            try
            {
                using (var db = new ApplicationDbContext())
                {
                    errors = new StringBuilder();
                    Message         message = GetMessageById(messageId);
                    ApplicationUser user    = UserRoles.GetUserById(userId);
                    Post            post    = UserPost.GetPostById(message.postId);

                    if (!MessageAction.CanUpdateMessageDatabase(message, user, post))
                    {
                        errors.Append("Can't delete Message");
                        return;
                    }
                    message.Deleted = true;
                    db.Messages.AddOrUpdate(message);
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
コード例 #2
0
        public static void CreateMessage(int postId, string userId, string userMessage)
        {
            try
            {
                using (var db = new ApplicationDbContext())
                {
                    Post            post   = UserPost.GetPostById(postId);
                    ApplicationUser author = (from user in db.Users
                                              where user.Id.Equals(userId)
                                              select user).FirstOrDefault();
                    ApplicationUser receiver = (from user in db.Users
                                                where user.Id.Equals(post.Author.Id)
                                                select user).FirstOrDefault();
                    var message = new Message
                    {
                        Body       = userMessage,
                        SendTo     = receiver,
                        CreatedBy  = author,
                        CreateDate = DateTime.Now,
                        postId     = postId,
                        Deleted    = false
                    };

                    post.Messages.Add(message);
                    db.Messages.Add(message);
                    db.SaveChanges();
                }
            }


            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }