示例#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));
        }
        public IHttpActionResult CreateArtwork(ArtworkDto artworkDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var artwork = Mapper.Map <ArtworkDto, Artwork>(artworkDto);

            _context.Artworks.Add(artwork);
            _context.SaveChanges();

            artworkDto.Id = artwork.Id;

            return(Created(new Uri(Request.RequestUri + "/" + artwork.Id), artworkDto));
        }
 public IActionResult AddArtwork([FromBody] ArtworkDto input)
 {
     try
     {
         Artwork insert = new Artwork()
         {
             Name        = input.Name,
             Description = input.Description,
             ImageUrl    = input.Url,
             UserID      = input.UserId
         };
         artworkBLL.Insert(insert);
         return(Ok(new { message = "Eser eklendi.", check = true }));
     }
     catch (Exception ex)
     {
         return(Ok(new { message = ex.Message, check = false }));
     }
 }
        public void TestGetArtworkByIdForAnArtist(int artistId, int artworkId, string title, int year, string source)
        {
            // Arrange
            //... done in the constructor

            // Act
            IActionResult actionResult = _cutArtworks.GetArtworkByIdForAnArtist(artistId, artworkId);

            // Assert
            Assert.NotNull(actionResult);

            OkObjectResult result = actionResult as OkObjectResult;

            Assert.NotNull(result);
            ArtworkDto artwork = result.Value as ArtworkDto;

            Assert.Equal(title, artwork.Title);
            Assert.Equal(year, artwork.Year);
            Assert.Equal(source, artwork.Source);
        }
        // PUT /api/Artworks/1
        public IHttpActionResult UpdateArtwork(int id, ArtworkDto artworkDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var artworkInDb = _context.Artworks.SingleOrDefault(c => c.Id == id);

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

            Mapper.Map(artworkDto, artworkInDb);

            _context.SaveChanges();

            return(Ok());
        }