public MusicFileJson(string json) { JObject jObject = JObject.Parse(json); JToken jMusicFile = jObject["musicFile"]; ID = (int)jMusicFile["ID"]; Genre = (string)jMusicFile["Genre"]; JToken jTitle = jMusicFile["Title"]; JToken jInterpret = jMusicFile["Interpret"]; Title = new Models.TitleDTO { ID = (int)jTitle["ID"], Name = (string)jTitle["Name"] }; Interpret = new Models.InterpretDTO { ID = (int)jInterpret["ID"], Name = (string)jInterpret["Name"] }; }
public IHttpActionResult Post() { try { using (MusicFileEntities entites = new MusicFileEntities()) { Models.MusicFileJson musicFile = new Models.MusicFileJson(Request.Content.ReadAsStringAsync().Result); // check if the genre is valid if (!(musicFile.Genre == "Pop" || musicFile.Genre == "Rock" || musicFile.Genre == "Dance" || musicFile.Genre == "Latin")) { return(Ok("Not a correct genre")); } //System.Diagnostics.Debug.WriteLine(musicFile.Title.Name); var join = (from m in entites.MusicFile join title in entites.Title on m.Title equals title.ID select new { ID = m.ID, Title = new Models.TitleDTO { ID = title.ID, Name = title.NAME }, Interpret = m.Interpret, Genre = title.Genre }); // check if the title is already in the database var element = join.FirstOrDefault(m => m.Title.Name == musicFile.Title.Name && m.Interpret == musicFile.Interpret.ID); if (element == null) { //First Create the title MusicFileDataAccess.Title title = new MusicFileDataAccess.Title(); title.NAME = musicFile.Title.Name; title.Genre = musicFile.Genre; entites.Title.Add(title); entites.SaveChanges(); // get the Title ID Models.TitleDTO createdTitle = entites.Title.Select(i => new Models.TitleDTO() { ID = i.ID, Name = i.NAME }).OrderByDescending(t => t.ID).FirstOrDefault(i => i.Name == musicFile.Title.Name); System.Diagnostics.Debug.WriteLine(createdTitle.ID); // if the interpret is not in the database, create a new interpret var existingInterpret = entites.Interpret.FirstOrDefault(i => i.NAME == musicFile.Interpret.Name); Models.InterpretDTO createdInterpret = new Models.InterpretDTO { ID = musicFile.Interpret.ID, Name = musicFile.Interpret.Name }; if (existingInterpret == null) { MusicFileDataAccess.Interpret interpret = new MusicFileDataAccess.Interpret(); interpret.NAME = musicFile.Interpret.Name; entites.Interpret.Add(interpret); entites.SaveChanges(); // after the creation get the newly id of the interpret createdInterpret = entites.Interpret.Select(i => new Models.InterpretDTO() { ID = i.ID, Name = i.NAME }).SingleOrDefault(i => i.Name == musicFile.Interpret.Name); } System.Diagnostics.Debug.WriteLine(createdTitle.ID); //in the last step create the music file MusicFileDataAccess.MusicFile createdMusicFile = new MusicFileDataAccess.MusicFile(); createdMusicFile.Title = createdTitle.ID; createdMusicFile.Interpret = createdInterpret.ID; entites.MusicFile.Add(createdMusicFile); entites.SaveChanges(); // return success message return(Ok("Music file successfully created!")); } else { return(Ok("Element is already in the database")); } } } catch (Exception ex) { return(BadRequest()); } }