Example #1
0
        public static int SaveUserComment(DBUserComment comment)
        {
            int id = comment.Id;

            using (ApplicationContext db = new ApplicationContext())
            {
                var dBUserComment = db.UserComments
                                    .FirstOrDefault(
                    x => x.Id == comment.Id &&
                    x.UserId == comment.UserId);
                if (id < 1)
                {
                    id               = db.UserComments.OrderBy(x => x.Id).LastOrDefault()?.Id ?? 0;
                    id               = ++id;
                    comment.Id       = id;
                    comment.UpdateDT = comment.CreateDT;
                    db.UserComments.Add(comment);
                }
                else
                {
                    if (comment == null)
                    {
                        throw new Exception($"Нет записи user с таким Id = {comment.Id} и UserId = {comment.UserId}");
                    }
                    comment.UpdateDT = DateTime.Now;
                    db.Entry(dBUserComment).CurrentValues.SetValues(comment);
                }

                db.SaveChanges();
            }
            return(id);
        }
Example #2
0
        public static DBUserComment GetUserComment(int id)
        {
            var userComment = new DBUserComment();

            using (ApplicationContext db = new ApplicationContext())
            {
                userComment         = db.UserComments.Where(x => x.Id == id).Include(x => x.User).Include(x => x.CreateUser).FirstOrDefault();
                userComment.Invoits = db.UserCommentInvoits.Where(x => x.UserCommentId == userComment.Id).Include(x => x.User).ToList();
            }
            return(userComment);
        }