Beispiel #1
0
        public async Task <ActionResult> Create(AlbumCreateViewModel albumCreateViewModel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "Model is not valid");
                return(View());
            }
            try
            {
                var newModel = await _albumService.AddAsync(new Album
                {
                    Artist      = _htmlEncoder.Encode(albumCreateViewModel.Artist),
                    Title       = albumCreateViewModel.Title,
                    CoverUrl    = albumCreateViewModel.CoverUrl,
                    Price       = albumCreateViewModel.Price,
                    ReleaseDate = albumCreateViewModel.ReleaseDate
                });

                return(RedirectToAction(nameof(Details), new { id = newModel.Id }));
            }
            catch
            {
                return(View(albumCreateViewModel));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Create(AlbumCreateViewModel albumCreateViewModel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "創建失敗");
                return(View());
            }

            var entity = _mapper.Map <Album>(albumCreateViewModel);
            await _albumService.AddAsync(entity);

            return(RedirectToAction("Details", new { id = entity.Id }));
        }
Beispiel #3
0
        public async Task AddAsync_CreateNewAlbum_Ok()
        {
            //Arrange
            var expected = AlbumApplicationStub.album_AddOrUpdate;

            //Act
            var actual = await _albumService.AddAsync(expected);

            //Assert
            actual.Should().BeOfType(typeof(AlbumApplication));
            actual.Should().NotBeNull();
            actual.Should().Equals(expected);
            actual.Id.Should().NotBe(0);
        }
        public async Task <ActionResult <AlbumApplication> > Post(
            [FromBody] AlbumApplication model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var result = await _albumService.AddAsync(model);

                return(Created("", result));
            }
            catch (Exception ex)
            {
                return(Conflict(ex.Message));

                throw;
            }
        }