//========================================================================== // GET: api/Musician/5 public vmMusician Get(int id) { if (ModelState.IsValid) { vmMusician vmM = service.GetMusicianById(id); if (vmM == null) { BadRequest("No musician exits inthe data base with the index of id."); return(null); } else { return(vmM); } } else { BadRequest("This data is not valid."); return(null); } }
//========================================================================== // POST: api/Musician public vmMusician Post([FromBody] vmMusician vmM) { vmMusician vmMus = new vmMusician(); if (ModelState.IsValid) { vmMus = service.PostMusician(vmM); if (vmMus == null) { BadRequest("AppUserId is not zero and it does not exist in the database."); return(null); } else { return(vmM); } } else { BadRequest("This data is not valid."); return(null); } }
//========================================================================== //Get a single musician by querying the database with MusicianId. public vmMusician GetMusicianById(int id) { Musician Mus = _repo.Query <Musician>().Where(a => a.MusicianId == id).FirstOrDefault(); if (Mus == null) { return(null); } else { var vmMus = new vmMusician { MusicianId = Mus.MusicianId, StageName = Mus.StageName, CellPhone = Mus.CellPhone, Biography = Mus.Biography, Rate = Mus.Rate, Rating = Mus.Rating, IsMusicianForHire = Mus.IsMusicianForHire }; return(vmMus); } }
//================================================================= //Methods(). //================================================================= //1. Add a new musician to the Musicians table. MusicianId = 0. //2. Update an exsiting musician. MusicianId != 0. public vmMusician PostMusician(vmMusician vmM) { Musician mus = new Musician { MusicianId = vmM.MusicianId, StageName = vmM.StageName, CellPhone = vmM.CellPhone, Biography = vmM.Biography, Rate = vmM.Rate, Rating = vmM.Rating, IsMusicianForHire = vmM.IsMusicianForHire }; if (mus.MusicianId == 0) { _repo.Add(mus); } else { _repo.Update(mus); } return(vmM); }