Exemplo n.º 1
0
        public SongFullModel GetById(int id)
        {
            if (id <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The song is invalid");
                throw new HttpResponseException(errResponse);
            }

            var entity = this.songRepository.Get(id);

            var model = new SongFullModel()
            {
                Id = entity.Id,
                Title = entity.Title,
                Year=entity.Year,
                Genre = entity.Genre,   
                AlbumTitle=entity.Album.Title,
                ArtistName=entity.Artist.Name,
                ArtistDateOfBird = entity.Artist.DateOfBirth
            };
            return model;
        }
Exemplo n.º 2
0
        public HttpResponseMessage PostSong(SongDetailsModel model)
        {
            if (model == null || model.Title == null || model.Title.Length < 6)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The title of the Song should be at least 6 characters");
                return errResponse;
            }


            var entityAlbum = this.albumRepository.All().Where(x => x.Title == model.FromAlbum).FirstOrDefault();
            if (entityAlbum == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "This album doesn't exist!");
            }

            var entityArtist = this.artistRepository.All().Where(x => x.Name == model.ArtistName).FirstOrDefault();
            
            if (entityArtist == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "This artist doesn't exist!");
            }        
            
            var entityToAdd = new Song
            {
                Title = model.Title,
                Year = model.Year,
                Genre = model.Genre,
                Album = entityAlbum,
                Artist = entityArtist
            };

            var createdEntity = this.songRepository.Add(entityToAdd);
            
            var createdModel = new SongFullModel()
            {
                Id = createdEntity.Id,
                Title = createdEntity.Title,
                Year = createdEntity.Year,
                Genre = createdEntity.Genre,
                ArtistName = createdEntity.Artist.Name,
                AlbumTitle = createdEntity.Album.Title
            };

            var response = Request.CreateResponse<SongFullModel>(HttpStatusCode.Created, createdModel);
            var resourceLink = Url.Link("DefaultApi", new { Id = createdModel.Id});
            response.Headers.Location = new Uri(resourceLink);      
            return response;
        }