コード例 #1
0
        public IActionResult CreateArtwork(int artistId, [FromBody] ArtworkForCreationDto artwork)
        {
            if (artwork == null)
            {
                return(BadRequest());
            }

            if (artwork.ShortDescription == artwork.Title)
            {
                ModelState.AddModelError("Short Description", "The provided short description should be different from the title.");
            }

            if (artwork.LongDescription == artwork.Title)
            {
                ModelState.AddModelError("Long Description", "The provided long description should be different from the title.");
            }

            if (artwork.LongDescription == artwork.ShortDescription)
            {
                ModelState.AddModelError("Long Description", "The provided long description should be different from the short description.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var artist = InMemoryDataStore.Current.Artists.FirstOrDefault(a => a.Id == artistId);

            if (artist == null)
            {
                return(NotFound());
            }

            // get the next artwork Id - to be improved
            var maxArtworkId  = InMemoryDataStore.Current.Artists.SelectMany(a => a.Artworks).Any() ? InMemoryDataStore.Current.Artists.SelectMany(a => a.Artworks).Max(i => i.Id) : 0;
            var nextArtworkId = ++maxArtworkId;

            var newArtwork = new ArtworkDto()
            {
                Id                = nextArtworkId,
                Title             = artwork.Title,
                Year              = artwork.Year,
                ShortDescription  = artwork.ShortDescription,
                LongDescription   = artwork.LongDescription,
                ImageUrl          = artwork.ImageUrl,
                ImageThumbnailUrl = artwork.ImageThumbnailUrl,
                Source            = artwork.Source,
            };

            artist.Artworks.Add(newArtwork);

            return(CreatedAtRoute("GetArtworkByIdForAnArtist", new { artistId, artworkId = newArtwork.Id }, newArtwork));
        }
コード例 #2
0
        public void TestCreateArtworkForANonExistingArtist(int artistId)
        {
            // Arrange
            ArtworkForCreationDto artwork = new ArtworkForCreationDto
            {
                Title             = "The Garden of Earthly Delights",
                Year              = 1505,
                ShortDescription  = "Is the modern title given to a triptych oil painting on oak panel painted by the Early Netherlandish master Hieronymus Bosch, housed in the Museo del Prado in Madrid since 1939. It dates from between 1490 and 1510, when Bosch was between 40 and 60 years old.",
                LongDescription   = "As so little is known of Bosch's life or intentions, interpretations of his intent have ranged from an admonition of worldly fleshy indulgence, to a dire warning on the perils of life's temptations, to an evocation of ultimate sexual joy. The intricacy of its symbolism, particularly that of the central panel, has led to a wide range of scholarly interpretations over the centuries. Twentieth-century art historians are divided as to whether the triptych's central panel is a moral warning or a panorama of paradise lost. Peter S. Beagle describes it as an 'erotic derangement that turns us all into voyeurs, a place filled with the intoxicating air of perfect liberty'.",
                ImageUrl          = "/images/Artworks/999-Jheronimus-Bosch/1505-The-Garden-of-Earthly-Delights.jpg",
                ImageThumbnailUrl = "/images/Artworks/999-Jheronimus-Bosch/1505-The-Garden-of-Earthly-Delights_tn.jpg",
                Source            = "www.wikipedia.org"
            };

            // Act
            IActionResult actionResult = _cutArtworks.CreateArtwork(artistId, artwork);

            // Assert
            NotFoundResult result = actionResult as NotFoundResult;

            Assert.Equal(404, result.StatusCode);
        }
コード例 #3
0
        public void TestCreateArtworkForAnExistingArtist(int artistId)
        {
            // Arrange
            ArtworkForCreationDto artwork = new ArtworkForCreationDto
            {
                Title             = "Bedroom in Arles",
                Year              = 1888,
                ShortDescription  = "Van Gogh's own title for this composition was simply The Bedroom (French: La Chambre à coucher). There are three authentic versions described in his letters, easily discernible from one another by the pictures on the wall to the right.",
                LongDescription   = "The painting depicts van Gogh's bedroom at 2, Place Lamartine in Arles, Bouches-du-Rhône, France, known as the Yellow House. The door to the right opened on to the upper floor and the staircase; the door to the left was that of the guest room he held prepared for Gauguin; the window in the front wall looked on to Place Lamartine and its public gardens. This room was not rectangular but trapezoid with an obtuse angle in the left hand corner of the front wall and an acute angle at the right.",
                ImageUrl          = "/images/Artworks/02-Vincent-van-Gogh/1888-Bedroom-in-Arles.jpg",
                ImageThumbnailUrl = "/images/Artworks/02-Vincent-van-Gogh/1888-Bedroom-in-Arles_tn.jpg",
                Source            = "www.wikipedia.org"
            };

            // Act
            IActionResult actionResult = _cutArtworks.CreateArtwork(artistId, artwork);

            // Assert
            Assert.NotNull(actionResult);
            CreatedAtRouteResult result = actionResult as CreatedAtRouteResult;

            Assert.NotNull(result);
            Assert.Equal(201, result.StatusCode);
        }