Example #1
0
        public HttpResponseMessage AddCommentToSong([FromUri] long id, [FromBody] CommentSongModel commentModel)
        {
            if (commentModel.DataId != id)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu không phù hợp"));
            }
            using (var db = new OnlineMusicEntities())
            {
                SongComment comment = new SongComment();
                commentModel.UpdateEntity(comment);
                comment.Date = DateTime.Now;
                db.SongComments.Add(comment);
                db.SaveChanges();

                comment.User = (from u in db.Users where u.Id == comment.UserId select u).FirstOrDefault();
                commentModel = commentDto.GetCommentQuery(db, swhereClause: null).Where(c => c.Id == comment.Id).FirstOrDefault();
                try
                {
                    Song song = (from s in db.Songs where s.Id == id select s).FirstOrDefault();
                    if (song != null && song.AuthorId != comment.UserId)
                    {
                        string       action       = NotificationAction.COMMENT_AUDIO + "_" + song.Id;
                        Notification notification = (from ntf in db.Notifications where ntf.UserId == song.AuthorId && ntf.Action == action select ntf).FirstOrDefault();
                        if (notification == null)
                        {
                            notification = new Notification()
                            {
                                Title  = "Hệ thống",
                                IsMark = false,
                                Action = action,
                                UserId = song.AuthorId
                            };
                            db.Notifications.Add(notification);
                        }
                        UserInfoModel info         = commentModel.UserInfo;
                        string        actor        = info != null && !String.IsNullOrEmpty(info.FullName) ? info.FullName : comment.User?.Username;
                        long          commentCount = song.SongComments.Select(c => c.UserId).Distinct().Count();
                        if (commentCount > 1)
                        {
                            actor += " và " + (commentCount - 1) + " người khác";
                        }
                        notification.Message = $"{actor} đã comment vào" +
                                               (song.Resource.Type == (int)ResourceTypeManager.Audio ? " bài hát " : " video ") + song.Title + " của bạn";
                        notification.CreatedAt = DateTime.Now;
                        db.SaveChanges();
                    }
                }
                catch
                {
                }

                return(Request.CreateResponse(HttpStatusCode.Created, commentModel));
            }
        }
Example #2
0
 public HttpResponseMessage EditComment([FromUri] long id, [FromBody] CommentSongModel commentModel)
 {
     if (commentModel.DataId != id)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu không phù hợp"));
     }
     using (var db = new OnlineMusicEntities())
     {
         SongComment comment = (from c in db.SongComments
                                where c.Id == commentModel.Id
                                select c).FirstOrDefault();
         if (comment == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.OK, "Không tìm thấy comment id=" + commentModel.Id));
         }
         commentModel.UpdateEntity(comment);
         db.SaveChanges();
         commentModel = commentDto.GetCommentQuery(db, (SongComment c) => c.Id == comment.Id).FirstOrDefault();
         return(Request.CreateResponse(HttpStatusCode.OK, commentModel));
     }
 }