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

                comment.User = (from u in db.Users where u.Id == comment.UserId select u).FirstOrDefault();
                commentModel = commentDto.GetCommentQuery(db, pwhereClause: null).Where(c => c.Id == comment.Id).FirstOrDefault();

                // Push notification
                try
                {
                    Playlist playlist = (from pl in db.Playlists where pl.Id == id select pl).FirstOrDefault();
                    if (playlist != null && comment.UserId != playlist.UserId)
                    {
                        string       action       = NotificationAction.COMMENT_PLAYLIST + "_" + playlist.Id;
                        Notification notification = (from ntf in db.Notifications where ntf.UserId == playlist.UserId && ntf.Action == action select ntf).FirstOrDefault();
                        if (notification == null)
                        {
                            notification = new Notification()
                            {
                                Title  = "Hệ thống",
                                IsMark = false,
                                Action = action,
                                UserId = playlist.UserId
                            };
                            db.Notifications.Add(notification);
                        }
                        UserInfoModel info         = commentModel.UserInfo;
                        string        actor        = info != null && !String.IsNullOrEmpty(info.FullName) ? info.FullName : comment.User?.Username;
                        long          commentCount = playlist.PlaylistComments.Select(c => c.UserId).Distinct().Count();
                        if (commentCount > 1)
                        {
                            actor += " và " + (commentCount - 1) + " người khác";
                        }
                        notification.Message   = $"{actor} đã comment vào playlist " + playlist.Title + " của bạn";
                        notification.CreatedAt = DateTime.Now;
                        db.SaveChanges();
                    }
                }
                catch
                {
                }
                return(Request.CreateResponse(HttpStatusCode.Created, commentModel));
            }
        }
Example #2
0
 public CommentPlaylistModel Converter(PlaylistComment c)
 {
     return(new CommentPlaylistModel()
     {
         Id = c.Id,
         Comment = c.Comment,
         UserId = c.UserId,
         Date = c.Date,
         DataId = c.PlaylistId,
         UserInfo = userInfoDto.ConvertToUserInfoModel(c.User.UserInfoes).FirstOrDefault()
     });
 }
Example #3
0
 public HttpResponseMessage DeleteComment([FromUri] int playlistId, [FromUri] long commentId)
 {
     using (var db = new OnlineMusicEntities())
     {
         PlaylistComment comment = (from c in db.PlaylistComments
                                    where c.Id == commentId
                                    select c).FirstOrDefault();
         if (comment == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.OK, "Không tìm thấy comment id=" + commentId));
         }
         db.PlaylistComments.Remove(comment);
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Example #4
0
 public HttpResponseMessage EditComment([FromUri] int id, [FromBody] CommentPlaylistModel commentModel)
 {
     if (commentModel.DataId != id)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu không phù hợp"));
     }
     using (var db = new OnlineMusicEntities())
     {
         PlaylistComment comment = (from c in db.PlaylistComments
                                    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, pwhereClause: null).Where(c => c.Id == comment.Id).FirstOrDefault();
         return(Request.CreateResponse(HttpStatusCode.OK, commentModel));
     }
 }