Пример #1
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;
            }
        }