コード例 #1
0
        public void can_serialize_library()
        {
            //// Arrange
            const string libraryFileName = @"library.xml";
            var          track           = new Track
            {
                Id       = new Guid("{DF565C73-D3F0-4DC4-B9C4-5F9915A13921}"),
                Title    = "Title",
                Added    = new DateTimeOffset(2015, 11, 6, 13, 14, 15, new TimeSpan(1, 0, 0)),
                Duration = new TimeSpan(1042),
                Year     = null,
                Location = @"C:\Some\Path"
            };
            var playlist = new Playlist
            {
                Id    = new Guid("{B3D01D19-DC13-4F38-AE38-1176DBB2A094}"),
                Name  = "Playlist",
                Items = { new PlaylistItem {
                              SortNumber = 42, Track = track
                          } }
            };
            var library = new Library.Model.Library
            {
                Tracks    = { track },
                Playlists = { playlist }
            };
            var persister = new AppDataXmlPersister <Library.Model.Library>("");

            //// Act
            persister.Save(library, libraryFileName);

            //// Assert
            var deserializedLibrary = persister.Load(libraryFileName);

            deserializedLibrary.Tracks.First().Id.Should().Be(track.Id);
            deserializedLibrary.Tracks.First().Added.Should().Be(new DateTimeOffset(2015, 11, 6, 13, 14, 15, new TimeSpan(1, 0, 0)));
            deserializedLibrary.Tracks.First().Title.Should().Be(track.Title);
            deserializedLibrary.Tracks.First().Year.Should().NotHaveValue();
            deserializedLibrary.Tracks.First().Location.Should().Be(track.Location);

            deserializedLibrary.Playlists.First().Id.Should().Be(playlist.Id);
            deserializedLibrary.Playlists.First().Name.Should().Be(playlist.Name);
            deserializedLibrary.Playlists.First().Items.First().SortNumber.Should().Be(playlist.Items.First().SortNumber);
            deserializedLibrary.Playlists.First().Items.First().Track.Id.Should().Be(track.Id);
            deserializedLibrary.Playlists.First().Items.First().Track.Added.Should().Be(new DateTimeOffset(2015, 11, 6, 13, 14, 15, new TimeSpan(1, 0, 0)));
            deserializedLibrary.Playlists.First().Items.First().Track.Title.Should().Be(track.Title);
        }
コード例 #2
0
        public void can_serialize_library()
        {
            //// Arrange
            const string libraryFileName = @"library.xml";
            var track = new Track
            {
                Id = new Guid("{DF565C73-D3F0-4DC4-B9C4-5F9915A13921}"),
                Title = "Title",
                Added = new DateTimeOffset(2015, 11, 6, 13, 14, 15, new TimeSpan(1, 0, 0)),
                Duration = new TimeSpan(1042),
                Year = null,
                Location = @"C:\Some\Path"
            };
            var playlist = new Playlist
            {
                Id = new Guid("{B3D01D19-DC13-4F38-AE38-1176DBB2A094}"),
                Name = "Playlist",
                Items = { new PlaylistItem { SortNumber = 42, Track = track } }
            };
            var library = new Library.Model.Library
            {
                Tracks = { track },
                Playlists = { playlist }
            };
            var persister = new AppDataXmlPersister<Library.Model.Library>("");

            //// Act
            persister.Save(library, libraryFileName);

            //// Assert
            var deserializedLibrary = persister.Load(libraryFileName);
            deserializedLibrary.Tracks.First().Id.Should().Be(track.Id);
            deserializedLibrary.Tracks.First().Added.Should().Be(new DateTimeOffset(2015, 11, 6, 13, 14, 15, new TimeSpan(1, 0, 0)));
            deserializedLibrary.Tracks.First().Title.Should().Be(track.Title);
            deserializedLibrary.Tracks.First().Year.Should().NotHaveValue();
            deserializedLibrary.Tracks.First().Location.Should().Be(track.Location);

            deserializedLibrary.Playlists.First().Id.Should().Be(playlist.Id);
            deserializedLibrary.Playlists.First().Name.Should().Be(playlist.Name);
            deserializedLibrary.Playlists.First().Items.First().SortNumber.Should().Be(playlist.Items.First().SortNumber);
            deserializedLibrary.Playlists.First().Items.First().Track.Id.Should().Be(track.Id);
            deserializedLibrary.Playlists.First().Items.First().Track.Added.Should().Be(new DateTimeOffset(2015, 11, 6, 13, 14, 15, new TimeSpan(1, 0, 0)));
            deserializedLibrary.Playlists.First().Items.First().Track.Title.Should().Be(track.Title);
        }
コード例 #3
0
        public static async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Function, "post", "patch", "get", Route = null)]
            HttpRequest req, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            log.LogInformation(req.Method);
            var lib = new DuncleLibrary(
                new BlobDatabase(
                    Environment.GetEnvironmentVariable("StorageAccountConnectionString")));

            if (req.Method == "GET")
            {
                try
                {
                    List <Library.Model.Library> libraries = await lib.getLibraries();

                    return(new OkObjectResult(libraries));
                }
                catch (Exception e)
                {
                    var errorString = $"Failed to get the libraries with error {e}";
                    log.LogError(errorString);

                    return(new BadRequestObjectResult(errorString));
                }
            }


            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            Library.Model.Library inputLibrary;
            try
            {
                inputLibrary = JsonConvert.DeserializeObject <Library.Model.Library>(requestBody);
            }
            catch (Exception)
            {
                string errorMessage = $"failed to parse input body to library";
                log.LogError(errorMessage);
                return(new BadRequestObjectResult(errorMessage));
            }


            try
            {
                switch (req.Method)
                {
                case "POST":
                {
                    Library.Model.Library createdLibrary = await lib.createLibrary(inputLibrary);

                    return(new OkObjectResult(createdLibrary));
                }

                case "PATCH)":
                    await lib.updateLibrary(inputLibrary);

                    return(new OkResult());

                default:
                    return
                        (new BadRequestObjectResult("Invalid method type. Accepted Methods are post and patch"));
                }
            }
            catch (Exception e)
            {
                string errorMessage = $"Failed to create library with error ${e}";
                return(new BadRequestObjectResult(errorMessage));
            }
        }