public void Should_fire_serializer_with_correct_artist_if_file_does_not_exist()
 {
     _fileWrapper.Stub(x => x.FileExists("")).IgnoreArguments().Return(false);
     var artistWriter = new ArtistWriter(_fileWrapper, _serializer);
     var artist = new Artist(){Id=1, Genre="Rock",Name="Test"};
     artistWriter.CreateFile(artist);
     _serializer.AssertWasCalled(x => x.Serialize(artist));
 }
 public void Should_return_NoContent_on_successful_update()
 {
     var artistHandler = new ArtistHandler(_writer, _reader, _operationOutput);
     var artist = new Artist { Id = 1, Genre = "r", Name = "r" };
     _reader.Stub(x => x.ReadFromFile(0)).IgnoreArguments().Return(artist);
     var operationResult = artistHandler.Put(artist);
     Assert.That(operationResult.StatusCode, Is.EqualTo(204));
 }
 public OperationResult Get(Artist artist)
 {
     try {
         Artist fromFile = _reader.ReadFromFile(artist.Id);
         return new OperationResult.OK(fromFile);
     } catch(Exception ex) {
         return _operationOutput.HandleOutput(ex, artist);
     }
 }
        public void Should_fire_save_with_correct_xml_if_file_does_not_exist()
        {
            var xPathNavigable = MockRepository.GenerateStub<XmlDocument>();
            _fileWrapper.Stub(x => x.FileExists("")).IgnoreArguments().Return(false);
            _serializer.Stub(x => x.Serialize(null)).IgnoreArguments().Return(xPathNavigable);

            var artistWriter = new ArtistWriter(_fileWrapper, _serializer);
            var artist = new Artist() { Id = 1, Genre = "Rock", Name = "Test" };
            artistWriter.CreateFile(artist);
            _fileWrapper.AssertWasCalled(x => x.WriteFile(Arg<string>.Is.Anything, Arg<string>.Is.Anything));
        }
 public OperationResult Post(Artist artist)
 {
     try {
         var uriString = ServiceEnvironment.CreateArtistUriString(artist.Id);
         _writer.CreateFile(artist);
         return new OperationResult.Created
         {RedirectLocation = new Uri(uriString), ResponseResource = artist};
     }catch(Exception ex) {
         return _operationOutput.HandleOutput(ex, artist);
     }
 }
        public OperationResult Put(Artist artist)
        {
            try {
                var artistToUpdate = _reader.ReadFromFile(artist.Id);
                ReMapArtist(artist, artistToUpdate);

                _writer.UpdateFile(artistToUpdate);
                var uriString = ServiceEnvironment.CreateArtistUriString(artistToUpdate.Id);

                return new OperationResult.NoContent { ResponseResource = artistToUpdate, RedirectLocation = new Uri(uriString) };
            }catch(Exception ex) {
                return _operationOutput.HandleOutput(ex, artist);
            }
        }
        public OperationResult Delete(Artist artist)
        {
            try {
                string filePath = ServiceEnvironment.GetFilePath(artist.Id, "C:/artist");

                if (!_reader.Exists(filePath))
                    throw new FileNotFoundException(string.Format("Could not find {0}", filePath));

                _writer.DeleteFile(artist.Id);
                return new OperationResult.NoContent();
            }
            catch(Exception ex) {
                return _operationOutput.HandleOutput(ex, artist);
            }
        }
 private static void ReMapArtist(Artist fromArtist, Artist toArtist)
 {
     if(!string.IsNullOrEmpty(fromArtist.Name))
         toArtist.Name = fromArtist.Name;
     if (!string.IsNullOrEmpty(fromArtist.Genre))
         toArtist.Genre = fromArtist.Genre;
 }