public void TestCreateArtist()
        {
            // Arrange
            ArtistForCreationDto artist = new ArtistForCreationDto
            {
                Name              = "Jheronimus Bosch",
                City              = "Hertogenbosch",
                BirthYear         = 1450,
                Birth             = new DateTime(1568, 02, 25),
                DeathYear         = 1516,
                Death             = new DateTime(1890, 07, 29),
                ShortDescription  = "Was a Dutch/Netherlandish draughtsman and painter from Brabant.",
                LongDescription   = "He is widely considered one of the most notable representatives of Early Netherlandish painting school. His work is known for its fantastic imagery, detailed landscapes, and illustrations of religious concepts and narratives. Within his lifetime his work was collected in the Netherlands, Austria, and Spain, and widely copied, especially his macabre and nightmarish depictions of hell.",
                ImageUrl          = "/images/Artists/Jheronimus-Bosch.jpg",
                ImageThumbnailUrl = "/images/Artists/Jheronimus-Bosch_tn.jpg"
            };

            // Act
            IActionResult actionResult = _cutArtists.CreateArtist(artist);

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

            Assert.NotNull(result);
            Assert.Equal(201, result.StatusCode);
        }
        public IActionResult CreateArtist([FromBody] ArtistForCreationDto artist)
        {
            try
            {
                if (artist == null)
                {
                    _logger.LogError("Artist object sent from client is null.");
                    return(BadRequest("Artist object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid Artist object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                _logic.CreateArtist(artist);

                return(Ok("Artist is created"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }
        public IActionResult CreateArtist([FromBody] ArtistForCreationDto artist)
        {
            if (artist == null)
            {
                return(BadRequest());
            }

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

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

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

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

            // get the next artist Id - to be improved
            var maxArtistId  = InMemoryDataStore.Current.Artists.Any() ? InMemoryDataStore.Current.Artists.Max(i => i.Id) : 0;
            var nextArtistId = ++maxArtistId;

            var newArtist = new ArtistDto()
            {
                Id                 = nextArtistId,
                Name               = artist.Name,
                City               = artist.City,
                BirthYear          = artist.BirthYear,
                Birth              = artist.Birth,
                DeathYear          = artist.DeathYear,
                Death              = artist.Death,
                ShortDescription   = artist.ShortDescription,
                LongDescription    = artist.LongDescription,
                ImageUrl           = artist.ImageUrl,
                ImageThumbnailUrl  = artist.ImageThumbnailUrl,
                IsArtistOfTheMonth = (artist.Birth == null) ? false : (artist.Birth.Month == DateTime.Now.Month ? true : false),
            };

            InMemoryDataStore.Current.Artists.Add(newArtist);

            return(CreatedAtRoute("GetArtistById", new { artistId = newArtist.Id }, newArtist));
        }
Esempio n. 4
0
        public bool CreateArtist(ArtistForCreationDto artist)
        {
            try
            {
                var DataEntity = _mapper.Map <Artist>(artist);

                _repository.Create(DataEntity);
                _repository.Save();

                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateArtist action: {ex.Message}");
                throw new Exception();
            }
        }
        public ActionResult <ArtistDto> CreateArtist(
            ArtistForCreationDto artist)
        {
            var artistEntity = mapper.Map <Artist>(artist);

            repository.AddArtist(artistEntity);
            repository.Commit();
            var artistToReturn = mapper.Map <ArtistDto>(artistEntity);

            var links = CreateLinksForArtist(artistToReturn.Id, null);

            var linkedResource = artistToReturn.ShapeData(null)
                                 as IDictionary <string, object>;

            linkedResource.Add("links", links);

            return(CreatedAtRoute("GetArtist",
                                  new { artistId = linkedResource["Id"] },
                                  linkedResource));
        }
Esempio n. 6
0
        public async Task <IActionResult> CreateArtist([FromBody] ArtistForCreationDto artist, [FromHeader] string mediaType)
        {
            var artistEntity = Mapper.Map <Artist>(artist);

            artistEntity.Id = Guid.NewGuid();

            _artistRepository.Create(artistEntity);

            if (!await _artistRepository.SaveChangesAsync())
            {
                _logger.LogError("Creation of an artist failed on saving to database");
            }

            var artistToReturn = Mapper.Map <ArtistDto>(artistEntity);

            if (!string.IsNullOrWhiteSpace(mediaType) && mediaType == "application/vnd.BD.json+hateoas")
            {
                var shapedArtist = _controllerHelper.ShapeAndAddLinkToObject(artistToReturn, "Artist", null);
                return(CreatedAtRoute("GetArtist", new { artistId = artistToReturn.Id }, shapedArtist));
            }

            return(CreatedAtRoute("GetArtist", new { artistId = artistToReturn.Id }, artistToReturn));
        }