Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        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);
            }
        }