Пример #1
0
        public HttpResponseMessage CreateSong([FromBody] SongModel songModel)
        {
            if (songModel.Artists.Count == 0)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Phải có ít nhất 1 nghệ sĩ"));
            }
            using (var db = new OnlineMusicEntities())
            {
                var user = (from u in db.Users where u.Id == songModel.AuthorId select u).FirstOrDefault();
                if (user == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy user id=" + songModel.AuthorId));
                }
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var song  = new Song();
                        var query = dto.GetSongQuery(db);

                        // Update artists of song
                        foreach (var artist in songModel.Artists)
                        {
                            var art = (from a in db.Artists
                                       where a.Id == artist.Id
                                       select a).FirstOrDefault();
                            if (art == null)
                            {
                                art = new Artist()
                                {
                                    FullName = artist.FullName, GenreId = artist.GenreId > 0 ? artist.GenreId : 1,
                                    Gender   = 0, DateOfBirth = null, Photo = GoogleDriveServices.DEFAULT_ARTIST, Verified = false
                                };
                                db.Artists.Add(art);
                                db.SaveChanges();
                            }
                            song.Artists.Add(art);
                        }

                        songModel.UpdateEntity(song);
                        song.UploadedDate = DateTime.Now;
                        song.Privacy      = song.Verified = song.Official = false;
                        db.Songs.Add(song);
                        db.SaveChanges();
                        transaction.Commit();
                        db.Entry(song).Reference(s => s.Genre).Load();
                        db.Entry(song).Reference(s => s.User).Load();
                        db.Entry(song).Collection(s => s.Artists).Load();

                        songModel = dto.GetSongQuery(db, s => s.Id == song.Id).FirstOrDefault();
                        return(Request.CreateResponse(HttpStatusCode.Created, songModel));
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
                    }
                }
            }
        }
Пример #2
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));
            }
        }
Пример #3
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));
            }
        }
Пример #4
0
 public HttpResponseMessage FollowArtist([FromUri] int artistId, [FromUri] int userId, bool unfollow = false)
 {
     using (var db = new OnlineMusicEntities())
     {
         var artist = (from a in db.Artists
                       where a.Id == artistId
                       select a).SingleOrDefault();
         if (artist == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy nghệ sĩ id=" + userId));
         }
         var follower = (from u in db.Users
                         where u.Id == userId
                         select u).SingleOrDefault();
         if (follower == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy follower user id=" + userId));
         }
         if (unfollow)
         {
             artist.Users.Remove(follower);
         }
         else
         {
             artist.Users.Add(follower);
         }
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #5
0
        public HttpResponseMessage UpdateUserInfo([FromBody] UserInfoModel userInfo)
        {
            var identity = (ClaimsIdentity)User.Identity;

            if (identity.Name != userInfo.UserId.ToString())
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Token"));
            }
            try
            {
                using (OnlineMusicEntities db = new OnlineMusicEntities())
                {
                    var user = (from u in db.UserInfoes
                                where u.Id == userInfo.Id
                                select u).FirstOrDefault();
                    if (user == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Thông tin người dùng này không tồn tại"));
                    }

                    userInfo.UpdateEntity(user);
                    db.SaveChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK, userInfo));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Пример #6
0
        public HttpResponseMessage Login([FromBody] UserLoginModel userLogin)
        {
            try
            {
                using (var db = new OnlineMusicEntities())
                {
                    bool success = false;
                    var  user    = (from u in db.Users
                                    where u.Username.ToLower() == userLogin.Username.ToLower()
                                    select u).FirstOrDefault();

                    if (user != null)
                    {
                        // Prevent if user is blocked
                        if (user.Blocked)
                        {
                            return(Request.CreateResponse(HttpStatusCode.Forbidden));
                        }

                        MemoryCacher cache         = new MemoryCacher();
                        string       cachePassword = cache.Get(user.Username) != null ? (string)cache.Get(user.Username) : String.Empty;
                        success = HashingPassword.ValidatePassword(userLogin.Password, user.Password);
                        if (!success)
                        {
                            success = !String.IsNullOrEmpty(cachePassword) && HashingPassword.ValidatePassword(userLogin.Password, cachePassword);
                            if (success)
                            {
                                Notification notification = new Notification()
                                {
                                    Title   = "Đăng nhập với mật khẩu tạm thời",
                                    Message = "Bạn vừa đăng nhập bằng mật khẩu tạm thời của mình vào " + DateTime.Now.ToString() +
                                              "\nNếu đây không phải là bạn, khuyên cáo bạn nên đổi lại mật khẩu của mình",
                                    UserId    = user.Id,
                                    IsMark    = false,
                                    CreatedAt = DateTime.Now,
                                    Action    = NotificationAction.LOGIN_TEMPORARILY
                                };
                                db.Notifications.Add(notification);
                                db.SaveChanges();
                            }
                        }
                    }

                    if (success)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, new UserModel {
                            User = user
                        }));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.Unauthorized));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Пример #7
0
        public HttpResponseMessage AddSongToPlaylist([FromUri] int playlistId, [FromUri] int songId)
        {
            using (var db = new OnlineMusicEntities())
            {
                var playlist = (from a in db.Playlists
                                where a.Id == playlistId
                                select a).FirstOrDefault();
                if (playlist == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy playlist id=" + playlistId));
                }
                // Identity user upload song to playlist
                var identity = (ClaimsIdentity)User.Identity;
                if (identity.Name != playlist.UserId.ToString())
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Token"));
                }

                var sg = (from s in db.Songs
                          where s.Id == songId
                          select s).FirstOrDefault();
                if (sg != null)
                {
                    playlist.Songs.Add(sg);
                }
                db.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
        }
Пример #8
0
        public HttpResponseMessage AddLyric([FromUri] long id, [FromBody] LyricModel lyricModel)
        {
            using (var db = new OnlineMusicEntities())
            {
                var query = dto.GetSongQuery(db);
                var song  = (from s in db.Songs
                             where s.Id == id
                             select s).FirstOrDefault();
                if (song == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy bài hát id=" + id));
                }
                if (song.Resource.Type == (int)ResourceTypeManager.Audio)
                {
                    Lyric lyric = new Lyric();
                    lyricModel.UpdateEntity(lyric);
                    db.Lyrics.Add(lyric);
                    db.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.Created));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Chỉ có thể thêm lời bài hát cho audio"));
                }
            }
        }
Пример #9
0
        public HttpResponseMessage AddScore([FromBody] ScoreModel scoreModel)
        {
            using (OnlineMusicEntities db = new OnlineMusicEntities())
            {
                var user = (from u in db.Users where u.Id == scoreModel.UserId select u).FirstOrDefault();
                if (user == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy user id=" + scoreModel.UserId));
                }

                var identity = (ClaimsIdentity)User.Identity;
                if (identity.Name != scoreModel.UserId.ToString())
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Token"));
                }

                Score score = (from s in db.Scores where s.UserId == user.Id select s).FirstOrDefault();
                if (score == null)
                {
                    score = new Score();
                    db.Scores.Add(score);
                }
                // Update new score if new score greater than old one
                if (scoreModel.Score > score.Score1)
                {
                    scoreModel.UpdateEntity(score);
                    db.SaveChanges();
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
        }
Пример #10
0
        public HttpResponseMessage VerifySongs([FromBody] List <SongModel> list)
        {
            using (var db = new OnlineMusicEntities())
            {
                foreach (SongModel model in list)
                {
                    Song song = (from s in db.Songs where s.Id == model.Id select s).FirstOrDefault();
                    if (song != null && song.Verified != model.Verified)
                    {
                        song.Verified = model.Verified;
                        db.SaveChanges();

                        if (song.Verified)
                        {
                            try
                            {
                                // Push notification
                                string       action       = NotificationAction.VERIFIED_MEDIA + "_" + song.Id;
                                Notification notification = (from ntf in db.Notifications where ntf.UserId == song.AuthorId select ntf).FirstOrDefault();
                                if (notification == null)
                                {
                                    notification = new Notification()
                                    {
                                        Title   = "Hệ thống đã kiểm duyệt",
                                        Message = song.Resource.Type == (int)ResourceTypeManager.Audio ? "Bài hát " : "Video " + song.Title +
                                                  " của bạn đã được kiểm duyệt thành công",
                                        IsMark    = false,
                                        UserId    = song.AuthorId,
                                        CreatedAt = DateTime.Now,
                                        Action    = action
                                    };
                                    db.Notifications.Add(notification);
                                    db.SaveChanges();
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
        }
Пример #11
0
        public HttpResponseMessage ChangePassword([FromUri] int id, PasswordModel passwordModel)
        {
            var identity = (ClaimsIdentity)User.Identity;

            if (identity.Name != id.ToString())
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Token"));
            }
            using (var db = new OnlineMusicEntities())
            {
                try
                {
                    var user = (from u in db.Users
                                where u.Id == id
                                select u).FirstOrDefault();

                    if (user == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"Tài khoản với id={id} không tồn tại"));
                    }
                    else
                    {
                        MemoryCacher cache         = new MemoryCacher();
                        string       cachePassword = cache.Get(user.Username) != null ? (string)cache.Get(user.Username) : String.Empty;
                        bool         isValid       = HashingPassword.ValidatePassword(passwordModel.OldPassword, user.Password);
                        if (!isValid)
                        {
                            // Try check cache password
                            isValid = !String.IsNullOrEmpty(cachePassword) && HashingPassword.ValidatePassword(passwordModel.OldPassword, cachePassword);
                        }

                        if (!isValid)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Mật khẩu cũ không đúng"));
                        }
                        else
                        {
                            user.Password = HashingPassword.HashPassword(passwordModel.NewPassword);
                            cache.Delete(user.Username);
                            db.SaveChanges();
                            return(Request.CreateResponse(HttpStatusCode.OK));
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
                }
            }
        }
Пример #12
0
 public HttpResponseMessage CreatePlaylist([FromBody] PlaylistModel playlistModel)
 {
     using (var db = new OnlineMusicEntities())
     {
         var playlist = new Playlist();
         playlistModel.UpdateEntity(playlist);
         playlist.CreatedDate = DateTime.Now;
         playlist.Photo       = GoogleDriveServices.DEFAULT_PLAYLIST;
         db.Playlists.Add(playlist);
         db.SaveChanges();
         db.Entry(playlist).Reference(pl => pl.User).Load();
         playlistModel = dto.GetPlaylistQuery(db, a => a.Id == playlist.Id).FirstOrDefault();
         return(Request.CreateResponse(HttpStatusCode.Created, playlistModel));
     }
 }
Пример #13
0
 public HttpResponseMessage OfficialSongs([FromBody] List <SongModel> list)
 {
     using (var db = new OnlineMusicEntities())
     {
         foreach (SongModel model in list)
         {
             Song song = (from s in db.Songs where s.Id == model.Id select s).FirstOrDefault();
             if (song != null && song.Official != model.Official)
             {
                 song.Official = model.Official;
                 db.SaveChanges();
             }
         }
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #14
0
 public HttpResponseMessage DeleteComment([FromUri] int albumId, [FromUri] long commentId)
 {
     using (var db = new OnlineMusicEntities())
     {
         AlbumComment comment = (from c in db.AlbumComments
                                 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.AlbumComments.Remove(comment);
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #15
0
 public HttpResponseMessage SetPrivacySongs([FromUri] int userId, [FromBody] List <SongModel> list)
 {
     using (var db = new OnlineMusicEntities())
     {
         foreach (SongModel model in list)
         {
             var song = (from s in db.Songs where s.Id == model.Id select s).FirstOrDefault();
             if (song != null)
             {
                 song.Privacy = model.Privacy;
             }
             db.SaveChanges();
         }
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #16
0
        public HttpResponseMessage CreateAlbum([FromBody] AlbumModel albumModel)
        {
            using (var db = new OnlineMusicEntities())
            {
                var album = new Album();
                albumModel.UpdateEntity(album);
                album.Photo = GoogleDriveServices.DEFAULT_ALBUM;
                db.Albums.Add(album);
                db.SaveChanges();
                db.Entry(album).Reference(a => a.Genre).Load();
                db.Entry(album).Reference(a => a.Artist).Load();

                albumModel = dto.Converter(db.Albums.Where(a => a.Id == album.Id).FirstOrDefault());
                return(Request.CreateResponse(HttpStatusCode.Created, albumModel));
            }
        }
Пример #17
0
 public HttpResponseMessage DeleteAlbum([FromUri] int id)
 {
     using (var db = new OnlineMusicEntities())
     {
         var album = (from a in db.Albums
                      where a.Id == id
                      select a).FirstOrDefault();
         if (album == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy album id=" + id));
         }
         db.Albums.Remove(album);
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #18
0
        public HttpResponseMessage IncreaseView([FromUri] int id)
        {
            using (var db = new OnlineMusicEntities())
            {
                var album = (from a in db.Albums
                             where a.Id == id
                             select a).FirstOrDefault();
                if (album == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy album id=" + id));
                }
                else if (album.Songs.Count > 0)
                {
                    var views = (from v in db.AlbumViews where v.AlbumId == id select v).FirstOrDefault();
                    // Create new if song hasn't view yet
                    if (views == null)
                    {
                        views = new AlbumView()
                        {
                            Ip = "", AlbumId = id, Timestamp = DateTime.Now, Views = 0
                        };
                        db.AlbumViews.Add(views);
                    }
                    else
                    {
                        // Reset view hour
                        if (views.Timestamp.Date.CompareTo(DateTime.Now.Date) != 0 || views.Timestamp.Hour != DateTime.Now.Hour)
                        {
                            views.Ip        = "";
                            views.Timestamp = DateTime.Now;
                        }
                    }

                    string ip = HttpContext.Current.Request.UserHostAddress.Trim();
                    // If IP hasn't view yet then increase view of song
                    if (!views.Ip.Contains(ip))
                    {
                        views.Ip += " " + ip;
                        views.Views++;
                    }

                    db.SaveChanges();
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
        }
Пример #19
0
        public HttpResponseMessage UpdateAlbum([FromBody] AlbumModel albumModel)
        {
            using (var db = new OnlineMusicEntities())
            {
                var album = (from a in db.Albums
                             where a.Id == albumModel.Id
                             select a).FirstOrDefault();
                if (album == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy album id=" + albumModel.Id));
                }

                albumModel.UpdateEntity(album);
                db.SaveChanges();
                albumModel = dto.Converter(album);
                return(Request.CreateResponse(HttpStatusCode.OK, albumModel));
            }
        }
Пример #20
0
        public HttpResponseMessage UpdatePlaylist([FromBody] PlaylistModel playlistModel)
        {
            using (var db = new OnlineMusicEntities())
            {
                var playlist = (from a in db.Playlists
                                where a.Id == playlistModel.Id
                                select a).FirstOrDefault();
                if (playlist == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy playlist id=" + playlistModel.Id));
                }

                playlistModel.UpdateEntity(playlist);
                db.SaveChanges();
                playlistModel = dto.GetPlaylistQuery(db, pl => pl.Id == playlist.Id).FirstOrDefault();
                return(Request.CreateResponse(HttpStatusCode.OK, playlistModel));
            }
        }
Пример #21
0
        public HttpResponseMessage UpdateArtist([FromBody] ArtistModel artistModel)
        {
            using (var db = new OnlineMusicEntities())
            {
                var query  = dto.GetArtistQuery(db);
                var artist = (from a in db.Artists
                              where a.Id == artistModel.Id
                              select a).FirstOrDefault();
                if (artist == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy nghệ sĩ id=" + artistModel.Id));
                }

                artistModel.UpdateEntity(artist);
                db.SaveChanges();
                artistModel = query.Where(a => a.Id == artist.Id).FirstOrDefault();
                return(Request.CreateResponse(HttpStatusCode.OK, artistModel));
            }
        }
Пример #22
0
 public HttpResponseMessage DeleteNotification([FromUri] int id, [FromUri] long notificationId)
 {
     using (var db = new OnlineMusicEntities())
     {
         var user = (from u in db.Users where u.Id == id select u).FirstOrDefault();
         if (user == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy user id=" + id));
         }
         var notify = (from ntf in user.Notifications
                       where ntf.Id == notificationId
                       select ntf).FirstOrDefault();
         if (notify != null)
         {
             db.Notifications.Remove(notify);
             db.SaveChanges();
         }
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #23
0
        public HttpResponseMessage UpdateLyric([FromUri] long id, [FromBody] LyricModel lyricModel)
        {
            using (var db = new OnlineMusicEntities())
            {
                var query = dto.GetSongQuery(db);
                var lyric = (from l in db.Lyrics
                             where l.Id == lyricModel.Id
                             select l).FirstOrDefault();
                if (lyric == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy lời bài hát id=" + lyricModel.Id));
                }

                lyric.Lyric1   = lyricModel.Lyric;
                lyric.Verified = lyricModel.Verified;
                db.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
        }
Пример #24
0
        public HttpResponseMessage AddCommentToAlbum([FromUri] int id, [FromBody] CommentAlbumModel commentModel)
        {
            if (commentModel.DataId != id)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu không phù hợp"));
            }
            using (var db = new OnlineMusicEntities())
            {
                AlbumComment comment = new AlbumComment();
                commentModel.UpdateEntity(comment);
                comment.Date = DateTime.Now;
                db.AlbumComments.Add(comment);
                db.SaveChanges();

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

                return(Request.CreateResponse(HttpStatusCode.Created, commentModel));
            }
        }
Пример #25
0
 public HttpResponseMessage DeleteNotifications([FromUri] int id, [FromBody] List <NotificationModel> list)
 {
     using (var db = new OnlineMusicEntities())
     {
         var user = (from u in db.Users where u.Id == id select u).FirstOrDefault();
         if (user == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy user id=" + id));
         }
         List <long> deletingId = (from model in list select model.Id).ToList();
         var         listNofity = (from ntf in user.Notifications
                                   where deletingId.Contains(ntf.Id)
                                   select ntf).ToList();
         if (listNofity != null && listNofity.Count > 0)
         {
             db.Notifications.RemoveRange(listNofity);
             db.SaveChanges();
         }
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #26
0
 public HttpResponseMessage EditComment([FromUri] int id, [FromBody] CommentAlbumModel commentModel)
 {
     if (commentModel.DataId != id)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu không phù hợp"));
     }
     using (var db = new OnlineMusicEntities())
     {
         AlbumComment comment = (from c in db.AlbumComments
                                 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, awhereClause: null).Where(c => c.Id == comment.Id).FirstOrDefault();
         return(Request.CreateResponse(HttpStatusCode.OK, commentModel));
     }
 }
Пример #27
0
        public HttpResponseMessage UpdateSong([FromBody] SongModel songModel)
        {
            using (var db = new OnlineMusicEntities())
            {
                var song = (from a in db.Songs
                            where a.Id == songModel.Id
                            select a).FirstOrDefault();
                if (song == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy nghệ sĩ id=" + songModel.Id));
                }

                // Update junction data
                if (songModel.Artists.Count > 0)
                {
                    song.Artists.Clear();
                    foreach (var artist in songModel.Artists)
                    {
                        var art = (from a in db.Artists
                                   where a.Id == artist.Id
                                   select a).FirstOrDefault();
                        if (art == null)
                        {
                            art = new Artist()
                            {
                                FullName    = artist.FullName, GenreId = artist.GenreId > 0 ? artist.GenreId : 1, Gender = 0,
                                DateOfBirth = null, Photo = GoogleDriveServices.DEFAULT_ARTIST, Verified = false
                            };
                        }
                        song.Artists.Add(art);
                    }
                }
                songModel.UpdateEntity(song);
                db.SaveChanges();
                songModel = dto.GetSongQuery(db, s => s.Id == song.Id).FirstOrDefault();
                return(Request.CreateResponse(HttpStatusCode.OK, songModel));
            }
        }
Пример #28
0
 public HttpResponseMessage DeleteLyric([FromUri] long songId, [FromUri] long lyricId)
 {
     using (var db = new OnlineMusicEntities())
     {
         var song = (from s in db.Songs
                     where s.Id == songId
                     select s).FirstOrDefault();
         if (song == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy bài hát id=" + songId));
         }
         var lyric = (from l in db.Lyrics
                      where l.Id == lyricId
                      select l).FirstOrDefault();
         if (lyric == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy lời bài hát id=" + lyricId));
         }
         db.Lyrics.Remove(lyric);
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }
Пример #29
0
        public HttpResponseMessage CreateArtist([FromBody] ArtistModel artistModel)
        {
            try
            {
                using (var db = new OnlineMusicEntities())
                {
                    Artist artist = new Artist();
                    artistModel.UpdateEntity(artist);
                    artist.Photo = GoogleDriveServices.DEFAULT_ARTIST;
                    db.Artists.Add(artist);
                    db.SaveChanges();
                    db.Entry(artist).Reference(a => a.Genre).Load();
                    db.Entry(artist).Collection(a => a.Users).Load();

                    artistModel = dto.GetArtistQuery(db, a => a.Id == artist.Id).FirstOrDefault();
                    return(Request.CreateResponse(HttpStatusCode.Created, artistModel));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Пример #30
0
 public HttpResponseMessage MarkNotifications([FromUri] int id, [FromBody] List <NotificationModel> list)
 {
     using (var db = new OnlineMusicEntities())
     {
         var user = (from u in db.Users where u.Id == id select u).FirstOrDefault();
         if (user == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy user id=" + id));
         }
         foreach (var model in list)
         {
             var notify = (from ntf in user.Notifications
                           where ntf.Id == model.Id
                           select ntf).FirstOrDefault();
             if (notify != null)
             {
                 notify.IsMark = true;
             }
         }
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
 }