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)); } }
public async Task <HttpResponseMessage> DownloadResource([FromUri] string id) { using (var db = new OnlineMusicEntities()) { var resource = (from r in db.Resources where r.Id == id select r).FirstOrDefault(); if (resource == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy resource")); } // If resource exists var stream = await services.Stream(id); HttpResponseMessage response = Request.CreateResponse(); response.Headers.AcceptRanges.Add("bytes"); response.StatusCode = HttpStatusCode.OK; response.Content = new StreamContent(stream); response.Content.Headers.Add("x-filename", resource.Name); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = resource.Name; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentLength = stream.Length; return(response); } }
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")); } } }
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)); } }
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)); } } } }
public void Execute(IJobExecutionContext context) { using (OnlineMusicEntities db = new OnlineMusicEntities()) { try { db.UpdateSongRanking(DateTime.Now.Date); Log("Updated song ranking"); } catch (Exception e) { Log("Failed to update song ranking: " + e.InnerException?.Message); } try { db.UpdateAlbumRanking(DateTime.Now.Date); Log("Updated album ranking"); } catch (Exception e) { Log("Failed to update album ranking: " + e.InnerException?.Message); } } }
public HttpResponseMessage GetPopularSongs(int page = 1, int size = 0, string type = "audio") { using (var db = new OnlineMusicEntities()) { IQueryable <SongModel> query = null; if (String.Compare(type, "audio", true) == 0) { query = dto.GetSongQuery(db, (song) => song.Resource.Type == (int)ResourceTypeManager.Audio); } else if (String.Compare(type, "video", true) == 0) { query = dto.GetSongQuery(db, (song) => song.Resource.Type == (int)ResourceTypeManager.Video); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Resource type not supported")); } List <SongModel> listSongs; if (size > 0) { listSongs = query.Where(s => !String.IsNullOrEmpty(s.ResourceId) && s.Verified == true && s.Privacy == false) .OrderByDescending(s => s.Views) .Skip((page - 1) * size) .Take(size).ToList(); } else { listSongs = query.Where(s => !String.IsNullOrEmpty(s.ResourceId) && s.Verified == true && s.Privacy == false) .OrderByDescending(s => s.Views).ToList(); } return(Request.CreateResponse(HttpStatusCode.OK, listSongs)); } }
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)); } }
public HttpResponseMessage GetUserInfo([FromUri] int id) { using (OnlineMusicEntities db = new OnlineMusicEntities()) { return(Request.CreateResponse(HttpStatusCode.OK, dto.GetUserInfoQuery(db, info => info.UserId == id).FirstOrDefault())); } }
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)); } }
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)); } }
public HttpResponseMessage GetAllRankingSongs() { using (var db = new OnlineMusicEntities()) { var rankingList = rankingAlbumDto.GetQueryRanking(db).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, rankingList)); } }
public HttpResponseMessage GetRankingSongs([FromUri] int year, [FromUri] int week) { using (var db = new OnlineMusicEntities()) { var rankingList = rankingAlbumDto.GetQueryRanking(db, r => r.StartDate.Year == year && r.Week == week).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, rankingList)); } }
public HttpResponseMessage GetRankingSongs([FromUri] int year, [FromUri] int month, [FromUri] int day) { using (var db = new OnlineMusicEntities()) { var rankingList = rankingSongDto.GetRankingQuery(db, r => r.StartDate.Year == year && r.StartDate.Month == month && r.StartDate.Day == day).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, rankingList)); } }
public HttpResponseMessage GetCommentsOfPlaylist([FromUri] int id) { using (var db = new OnlineMusicEntities()) { var listComments = commentDto.GetCommentQuery(db, (PlaylistComment c) => c.PlaylistId == id).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, listComments)); } }
public HttpResponseMessage GetResourcesByGenre(int genre) { using (OnlineMusicEntities db = new OnlineMusicEntities()) { var list = songDto.GetSongQuery(db, s => s.Privacy == false && s.Verified == true && s.Official == true && s.GenreId == genre).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, list)); } }
public HttpResponseMessage GetAllScore() { using (OnlineMusicEntities db = new OnlineMusicEntities()) { var list = dto.GetScoreQuery(db).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, list)); } }
public HttpResponseMessage GetHighScore(int size = 10) { using (OnlineMusicEntities db = new OnlineMusicEntities()) { var list = dto.GetScoreQuery(db) .OrderByDescending(s => s.Score).ThenBy(s => s.Id).Take(size).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, list)); } }
public HttpResponseMessage GetAllPlaylists() { using (var db = new OnlineMusicEntities()) { var query = dto.GetPlaylistQuery(db); var listPlaylists = query.OrderByDescending(pl => pl.Id).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, listPlaylists)); } }
public HttpResponseMessage GetPlaylists([FromUri] int id) { using (var db = new OnlineMusicEntities()) { var query = playlistDto.GetPlaylistQuery(db, playlist => playlist.UserId == id); var list = (from pl in query select pl).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, list)); } }
public HttpResponseMessage GetCommentsOfAlbum([FromUri] int id) { using (var db = new OnlineMusicEntities()) { var listComments = commentDto.GetCommentQuery(db, (AlbumComment c) => c.AlbumId == id) .OrderByDescending(c => c.Date).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, listComments)); } }
public HttpResponseMessage GetAllAlbums() { using (var db = new OnlineMusicEntities()) { var query = dto.GetAlbumQuery(db, null, true); var listAlbums = query.OrderByDescending(a => a.Id).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, listAlbums)); } }
public HttpResponseMessage GetRankingSongs([FromUri] int year, [FromUri] int month, [FromUri] int day) { using (var db = new OnlineMusicEntities()) { DateTime rankingDate = new DateTime(year, month, day); var rankingList = rankingAlbumDto.GetQueryRanking(db, r => r.StartDate.Equals(rankingDate)).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, rankingList)); } }
public HttpResponseMessage GetAllArtists() { using (var db = new OnlineMusicEntities()) { var query = dto.GetArtistQuery(db); IEnumerable <ArtistModel> listArtists; listArtists = query.OrderByDescending(a => a.Id).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, listArtists)); } }
public HttpResponseMessage GetAllArtists(bool verified) { using (var db = new OnlineMusicEntities()) { var query = dto.GetArtistQuery(db); IEnumerable <ArtistModel> listArtists; listArtists = query.Where(a => a.Verified == verified).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, listArtists)); } }
private bool IsEmailExisted(string email) { using (var db = new OnlineMusicEntities()) { var user = (from u in db.Users where u.Email.ToLower() == email.ToLower() select u).FirstOrDefault(); return(user != null); } }
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)); } }
public HttpResponseMessage GetUserInfo(int page = 1, int size = 20) { using (OnlineMusicEntities db = new OnlineMusicEntities()) { return(Request.CreateResponse(HttpStatusCode.OK, dto.GetUserInfoQuery(db) .OrderByDescending(info => info.Followers) .Skip((page - 1) * size) .Take(size) .ToList())); } }
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)); } }
public HttpResponseMessage GetAllGenres() { using (var db = new OnlineMusicEntities()) { var listGenres = (from genre in db.Genres select new GenreModel { Genre = genre }).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, listGenres)); } }