public IHttpActionResult ById(int id) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var dbArtist = this.data .Artists .All() .FirstOrDefault(a => a.Id == id); if (dbArtist == null) { return BadRequest("Such artist does not exist in database!"); } var artist = new ArtistRequestModel { BirthDate = dbArtist.BirthDate, CountryId = dbArtist.CountryId, Name = dbArtist.Name }; artist.Id = dbArtist.Id; return Ok(artist); }
// PUT: api/Producer/5 public IHttpActionResult Put(int id, ArtistRequestModel artist) { if (artist == null) { return(this.BadRequest("Invalid producer!")); } var artistToModify = this.artistData.All() .FirstOrDefault(a => a.ArtistId == id); var artistToAdd = new Artist { Name = artist.Name, BirthDate = artist.BirthDate }; var artistCountry = this.countryData.All() .FirstOrDefault(c => c.Name == artist.Country); if (artistCountry == null) { return(this.BadRequest("Invalid country name!")); } artistToAdd.Country = artistCountry; artistToModify.Name = artistToAdd.Name; artistToModify.Country = artistToAdd.Country; artistToModify.BirthDate = artistToAdd.BirthDate; this.artistData.SaveChanges(); return(this.Ok("Artist added")); }
public IHttpActionResult Create(ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var existingCountry = this.data .Countries .All() .FirstOrDefault(c => c.Id == artist.CountryId || c.Name == artist.Country.Name); if(existingCountry == null) { return BadRequest("Such country doen not exist in database!"); } var newArtist = new Artist { BirthDate = artist.BirthDate, Country = existingCountry, Name = artist.Name }; this.data.Artists.Add(newArtist); this.data.SaveChanges(); artist.Id = newArtist.Id; return Ok(artist); }
public IHttpActionResult Put(int id, ArtistRequestModel requestArtist) { var artist = Mapper.Map <Artist>(requestArtist); artist = this.artists.Update(id, artist); return(this.Ok(Mapper.Map <ArtistResponseModel>(artist))); }
public IHttpActionResult Post(ArtistRequestModel requestArtist) { var artist = Mapper.Map <Artist>(requestArtist); artist = this.artists.Add(artist); return(this.Created("/", Mapper.Map <ArtistResponseModel>(artist))); }
public IHttpActionResult Put(int id, ArtistRequestModel model) { var artistToUpdate = this.artists .All() .FirstOrDefault(a => a.Id == id); if (artistToUpdate == null) { return(this.NotFound()); } if (!this.ModelState.IsValid || model == null) { return(this.BadRequest(this.ModelState)); } var countryExists = this.countries .All() .Any(c => c.Id == model.CountryId); if (!countryExists) { return(this.BadRequest("Invalid country id")); } artistToUpdate.Name = model.Name; artistToUpdate.CountryId = model.CountryId; artistToUpdate.DateOfBirth = model.DateOfBirth; foreach (var songId in model.SongIds) { var currentSong = this.songs .All() .FirstOrDefault(s => s.Id == songId); if (currentSong != null) { artistToUpdate.Songs.Add(currentSong); } } foreach (var albumId in model.AlbumIds) { var currentAlbum = this.albums .All() .FirstOrDefault(a => a.Id == albumId); if (currentAlbum != null) { artistToUpdate.Albums.Add(currentAlbum); } } this.artists.SaveChanges(); return(this.Ok("The artist was successfully updated.")); }
/// <summary> /// Expect ArtistRequestModel with valid name, dateOfBirth (as string), CountryId , AlbumIds (array) - it can be empty, /// and SongIds (array) - it can be empty /// </summary> /// <param name="model"></param> /// <returns> /// If the artist is added returns IHttpActionResult with status code 200 OK /// If the input data in not in the valid format retunrs IHttpActionResult with status code 400 Bad Request /// </returns> public IHttpActionResult Post(ArtistRequestModel model) { if (!this.ModelState.IsValid || model == null) { return(this.BadRequest(this.ModelState)); } var countryExists = this.countries .All() .Any(c => c.Id == model.CountryId); if (!countryExists) { return(this.BadRequest("Invalid country id")); } var artistToAdd = new Artist() { Name = model.Name, DateOfBirth = model.DateOfBirth, CountryId = model.CountryId }; foreach (var songId in model.SongIds) { var currentSong = this.songs .All() .FirstOrDefault(s => s.Id == songId); if (currentSong != null) { artistToAdd.Songs.Add(currentSong); } } foreach (var albumId in model.AlbumIds) { var currentAlbum = this.albums .All() .FirstOrDefault(a => a.Id == albumId); if (currentAlbum != null) { artistToAdd.Albums.Add(currentAlbum); } } this.artists.Add(artistToAdd); this.artists.SaveChanges(); return(this.Ok("The artist was successfully added.")); }
public IHttpActionResult Post(ArtistRequestModel artist) { if (!ModelState.IsValid) { return(this.BadRequest()); } this.data.Artists.Add(new Artist { Name = artist.Name, Country = artist.Country, BirthDate = artist.BirthDate, }); this.data.SaveChanges(); return(this.Ok(artist.Name)); }
public IHttpActionResult Get() { // TODO does not work properly with Mapper so I did it by hand but needs more work var artists = db.Artists .AsQueryable() .ToList(); var artistModels = new List <ArtistRequestModel>(); foreach (var artist in artists) { var albums = new List <AlbumRequestModel>(); foreach (var album in artist.Albums) { var albumModel = new AlbumRequestModel { Producer = album.Producer, Title = album.Title, Year = album.Year }; albums.Add(albumModel); } var songs = new List <SongRequestModel>(); foreach (var song in artist.Songs) { var songToAdd = new SongRequestModel { Title = song.Title, GenreName = song.Genre.Name, Year = song.Year }; songs.Add(songToAdd); } var artistToAdd = new ArtistRequestModel { Name = artist.Name, Albums = albums, Songs = songs }; artistModels.Add(artistToAdd); } return(this.Ok(artistModels)); }
public IHttpActionResult Get() { // TODO does not work properly with Mapper so I did it by hand but needs more work var artists = db.Artists .AsQueryable() .ToList(); var artistModels = new List<ArtistRequestModel>(); foreach (var artist in artists) { var albums = new List<AlbumRequestModel>(); foreach (var album in artist.Albums) { var albumModel = new AlbumRequestModel { Producer = album.Producer, Title = album.Title, Year = album.Year }; albums.Add(albumModel); } var songs = new List<SongRequestModel>(); foreach (var song in artist.Songs) { var songToAdd = new SongRequestModel { Title = song.Title, GenreName = song.Genre.Name, Year = song.Year }; songs.Add(songToAdd); } var artistToAdd = new ArtistRequestModel { Name = artist.Name, Albums = albums, Songs = songs }; artistModels.Add(artistToAdd); } return this.Ok(artistModels); }
public IHttpActionResult Post(ArtistRequestModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var artist = new Artist { Name = model.Name, DateOfBirth = model.DateOfBirth }; this.data.Artists.Add(artist); this.data.Artists.SaveChanges(); return(this.Ok(artist)); }
public IHttpActionResult Update(int id, ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return(BadRequest(this.ModelState)); } var artistInDb = this.data.Artists .All() .FirstOrDefault(c => c.Id == id); artistInDb.Name = artist.Name; artistInDb.Country = artist.Country; artistInDb.DateOfBirth = artist.DateOfBirth; this.data.Artists.SaveChanges(); return(Ok(artistInDb)); }
public IHttpActionResult Post(ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var artistToPost = new Artist() { Name = artist.Name, DateOfBirth = artist.DateOfBirth, Country = artist.Country }; this.db.Artists.Add(artistToPost); this.db.SaveChanges(); return this.Ok(artist); }
public IHttpActionResult Create(ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return(BadRequest(this.ModelState)); } this.data .Artists .Add(new Artist { Name = artist.Name, Country = artist.Country, DateOfBirth = artist.DateOfBirth }); this.data.Artists.SaveChanges(); return(Ok(artist)); }
public IHttpActionResult Post([FromBody] ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var newArtist = new Artist { Name = artist.Name, Country = artist.Country, DateOfBirth = artist.DateOfBirth, Albums = artist.Albums }; this.repository.Insert(newArtist); this.repository.SaveChanges(); return(this.Ok(newArtist)); }
public IHttpActionResult Update(int id, ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return(BadRequest(this.ModelState)); } var country = this.data .Countries .All() .FirstOrDefault(c => c.Name.ToLower() == artist.Country.ToLower()); if (country == null) { country = new Country { Name = artist.Country }; this.data.Countries.Add(country); this.data.Countries.SaveChanges(); } var artistFromDatabase = this.data.Artists.All() .FirstOrDefault(a => a.Id == id); if (artistFromDatabase == null) { return(BadRequest("Artist not exist - invalid id")); } artistFromDatabase.Name = artist.Name; artistFromDatabase.DateOfBirth = artist.DateOfBirth; artistFromDatabase.CountryId = country.Id; this.data.Artists.SaveChanges(); return(Ok(artist)); }
public IHttpActionResult Put(int id, [FromBody] ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var currentArtist = this.repository.SelectById(id); if (currentArtist == null) { return(this.BadRequest("Invalid Id")); } currentArtist.Name = string.IsNullOrEmpty(artist.Name) ? currentArtist.Name : artist.Name; currentArtist.DateOfBirth = artist.DateOfBirth; currentArtist.Country = string.IsNullOrEmpty(artist.Country) ? currentArtist.Country : artist.Country; this.repository.Update(currentArtist); this.repository.SaveChanges(); return(this.Ok(currentArtist)); }
public IHttpActionResult Create(ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return(BadRequest(this.ModelState)); } var country = this.data .Countries .All() .FirstOrDefault(c => c.Name.ToLower() == artist.Country.ToLower()); if (country == null) { country = new Country { Name = artist.Country }; this.data.Countries.Add(country); this.data.Countries.SaveChanges(); } var newArtist = new Artist { Name = artist.Name, DateOfBirth = artist.DateOfBirth }; newArtist.CountryId = country.Id; this.data.Artists.Add(newArtist); this.data.Artists.SaveChanges(); return(Ok(artist)); }
public IHttpActionResult Put(int id, ArtistRequestModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var artist = this.data.Artists .All() .FirstOrDefault(a => a.Id == id); if (artist == null) { return(this.BadRequest("Artist with id " + id + " does not exist!")); } artist.Name = model.Name; artist.DateOfBirth = model.DateOfBirth; this.data.Artists.Update(artist); this.data.Artists.SaveChanges(); return(this.Ok(model)); }
public IHttpActionResult Put(int id, ArtistRequestModel model) { var artistToUpdate = this.artists .All() .FirstOrDefault(a => a.Id == id); if (artistToUpdate == null) { return this.NotFound(); } if (!this.ModelState.IsValid || model == null) { return this.BadRequest(this.ModelState); } var countryExists = this.countries .All() .Any(c => c.Id == model.CountryId); if (!countryExists) { return this.BadRequest("Invalid country id"); } artistToUpdate.Name = model.Name; artistToUpdate.CountryId = model.CountryId; artistToUpdate.DateOfBirth = model.DateOfBirth; foreach (var songId in model.SongIds) { var currentSong = this.songs .All() .FirstOrDefault(s => s.Id == songId); if (currentSong != null) { artistToUpdate.Songs.Add(currentSong); } } foreach (var albumId in model.AlbumIds) { var currentAlbum = this.albums .All() .FirstOrDefault(a => a.Id == albumId); if (currentAlbum != null) { artistToUpdate.Albums.Add(currentAlbum); } } this.artists.SaveChanges(); return this.Ok("The artist was successfully updated."); }
/// <summary> /// Expect ArtistRequestModel with valid name, dateOfBirth (as string), CountryId , AlbumIds (array) - it can be empty, /// and SongIds (array) - it can be empty /// </summary> /// <param name="model"></param> /// <returns> /// If the artist is added returns IHttpActionResult with status code 200 OK /// If the input data in not in the valid format retunrs IHttpActionResult with status code 400 Bad Request /// </returns> public IHttpActionResult Post(ArtistRequestModel model) { if (!this.ModelState.IsValid || model == null) { return this.BadRequest(this.ModelState); } var countryExists = this.countries .All() .Any(c => c.Id == model.CountryId); if (!countryExists) { return this.BadRequest("Invalid country id"); } var artistToAdd = new Artist() { Name = model.Name, DateOfBirth = model.DateOfBirth, CountryId = model.CountryId }; foreach (var songId in model.SongIds) { var currentSong = this.songs .All() .FirstOrDefault(s => s.Id == songId); if (currentSong != null) { artistToAdd.Songs.Add(currentSong); } } foreach (var albumId in model.AlbumIds) { var currentAlbum = this.albums .All() .FirstOrDefault(a => a.Id == albumId); if (currentAlbum != null) { artistToAdd.Albums.Add(currentAlbum); } } this.artists.Add(artistToAdd); this.artists.SaveChanges(); return this.Ok("The artist was successfully added."); }
public IHttpActionResult Update(int id, ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var dbArtist = this.data .Artists .All() .FirstOrDefault(a => a.Id == id); if (dbArtist == null) { return BadRequest("Such artist does not exist in database!"); } artist.BirthDate = dbArtist.BirthDate; artist.CountryId = dbArtist.CountryId; artist.Name = dbArtist.Name; this.data.SaveChanges(); artist.Id = id; return Ok(artist); }
public IHttpActionResult Put(int id, ArtistRequestModel artist) { if (!ModelState.IsValid) { return this.BadRequest(this.ModelState); } var artistToUpdate = this.db .Artists .All() .FirstOrDefault(a => a.Id == id); artistToUpdate.Name = artist.Name; artistToUpdate.DateOfBirth = artist.DateOfBirth; artistToUpdate.Country = artist.Country; this.db.SaveChanges(); return this.Ok(); }