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 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)); } }
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)); } }
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)); } }
public HttpResponseMessage GetScore([FromUri] int userId) { using (OnlineMusicEntities db = new OnlineMusicEntities()) { User user = (from u in db.Users where u.Id == userId select u).FirstOrDefault(); if (user == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Không tìm thấy user id=" + userId)); } Score score = (from s in db.Scores where s.UserId == userId select s).FirstOrDefault(); if (score == null) { score = new Score(); score.Score1 = 0; score.UserId = userId; db.Scores.Add(score); db.SaveChanges(); } db.Entry(score).Reference(s => s.User).Load(); ScoreModel model = dto.GetScoreQuery(db, s => s.UserId == userId).FirstOrDefault(); return(Request.CreateResponse(HttpStatusCode.OK, model)); } }