示例#1
0
        public async Task <IActionResult> AddNewGenre(GenreForCreatingDto dto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(dto));
                }

                await unitOfWork.Genres.AddAsync(dto);

                if (!await unitOfWork.SaveAsync())
                {
                    // ToDo: Implement error page
                    return(View("ErrorSaving"));
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "An error occurred while deleting song.");
            }
            // ToDo: Implement error page
            return(View("ErrorSaving"));
        }
示例#2
0
        public async Task Add_Genre_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                using (var context = factory.CreateMusicStoreContext())
                {
                    var unitOfWork = new UnitOfWork(context, _mapper);

                    var genre = new GenreForCreatingDto
                    {
                        Name = "Genre"
                    };

                    await unitOfWork.Genres.AddAsync(genre);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    Assert.NotNull(context.Genres.First());
                }
            }
        }
示例#3
0
        public async Task <IActionResult> CreateGenre([FromBody] GenreForCreatingDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                await unitOfWork.Genres.AddAsync(dto);

                if (!await unitOfWork.SaveAsync())
                {
                    throw new Exception("Creating Genre failed on save.");
                }

                return(NoContent());
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "An error occurred while creating genre.", dto);
                return(StatusCode(500));
            }
        }
示例#4
0
        public async Task AddAsync(GenreForCreatingDto dto)
        {
            var genreEntity = mapper.Map <GenreEntity>(dto);

            await context.Genres.AddAsync(genreEntity);
        }