Esempio n. 1
0
        public HttpResponseMessage PutAlbum(int id, Album albumEntity)
        {
            if (id <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   "The id of the album must be positive");
                return(errResponse);
            }

            try
            {
                var entity = this.albumRepository.Update(id, albumEntity);

                var response = this.Request.CreateResponse(HttpStatusCode.OK, AlbumModel.CreateFromAlbumEntity(entity));
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.AlbumId }));
                return(response);
            }
            catch (DbUpdateConcurrencyException)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                                   "The album could not be updated because of a concurrency problem");
                return(errResponse);
            }
            catch (NullReferenceException)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   "There is no album with id=" + id);
                return(errResponse);
            }
        }
Esempio n. 2
0
        public IEnumerable <AlbumModel> GetAll()
        {
            var entities = this.albumRepository.GetAll();

            List <AlbumModel> models = new List <AlbumModel>();

            foreach (var entity in entities)
            {
                models.Add(AlbumModel.CreateFromAlbumEntity(entity));
            }

            return(models);
        }
Esempio n. 3
0
        public AlbumModel GetById(int id)
        {
            if (id <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   "The id of the album must be positive");
                throw new HttpResponseException(errResponse);
            }

            var entity = this.albumRepository.GetById(id);

            if (entity != null)
            {
                AlbumModel model = AlbumModel.CreateFromAlbumEntity(entity);
                return(model);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 4
0
        public HttpResponseMessage PostAlbum(Album albumEntity)
        {
            if (albumEntity == null || albumEntity.Title == null)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The album is invalid");
                return(errResponse);
            }

            try
            {
                var entity = this.albumRepository.Add(albumEntity);

                var response = this.Request.CreateResponse(HttpStatusCode.Created, AlbumModel.CreateFromAlbumEntity(entity));
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.AlbumId }));
                return(response);
            }
            catch (DbUpdateConcurrencyException)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                                   "The album could not be added because of a concurrency problem");
                return(errResponse);
            }
        }