public HttpResponseMessage PutArtist(int id, Artist artistEntity) { if (id <= 0) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The id of the artist must be positive"); return(errResponse); } try { var entity = this.artistRepository.Update(id, artistEntity); var response = this.Request.CreateResponse(HttpStatusCode.OK, ArtistModel.CreateFromArtistEntity(entity)); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.ArtistsId })); return(response); } catch (DbUpdateConcurrencyException) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The artist could not be updated because of a concurrency problem"); return(errResponse); } catch (NullReferenceException) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "There is no artist with id=" + id); return(errResponse); } }
public IEnumerable <ArtistModel> GetAll() { var entities = this.artistRepository.GetAll(); List <ArtistModel> models = new List <ArtistModel>(); foreach (var entity in entities) { models.Add(ArtistModel.CreateFromArtistEntity(entity)); } return(models); }
public ArtistModel GetById(int id) { if (id <= 0) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The id of the artist must be positive"); throw new HttpResponseException(errResponse); } var entity = this.artistRepository.GetById(id); if (entity != null) { ArtistModel model = ArtistModel.CreateFromArtistEntity(entity); return(model); } else { return(null); } }
public HttpResponseMessage PostArtist(Artist artistEntity) { if (artistEntity == null || artistEntity.Name == null) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The artist is invalid"); return(errResponse); } try { var entity = this.artistRepository.Add(artistEntity); var response = this.Request.CreateResponse(HttpStatusCode.Created, ArtistModel.CreateFromArtistEntity(entity)); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.ArtistsId })); return(response); } catch (DbUpdateConcurrencyException) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The artist could not be added because of a concurrency problem"); return(errResponse); } }