public async Task <ActionResult <AlbumDTO> > PostAlbum([FromBody] AlbumAddDTO albumAddDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (albumAddDTO.UserId != UserId) { return(BadRequest()); } AlbumDTO albumDTO; try { albumDTO = await _albumService.AddAlbumAsync(albumAddDTO); } catch (Exception e) { return(BadRequest(e.Message)); } return(Ok(albumDTO)); }
public async Task <AlbumDTO> AddAlbumAsync(AlbumAddDTO albumDTO) { var album = mapper.Map <Album>(albumDTO, opt => opt.Items["creationTime"] = DateTime.Now); await unitOfWork.Albums.AddAsync(album); await unitOfWork.SaveAsync(); return(mapper.Map <AlbumDTO>(album)); }
public async Task AddAlbumAsync_Should_ReturnAlbumDTO() { var dto = new AlbumAddDTO { Name = "name", Description = "desc" }; mockUow.Setup(uow => uow.Albums.AddAsync(It.IsAny <Album>())); var result = await service.AddAlbumAsync(dto); result.Should().BeEquivalentTo(dto); result.Created.Should().NotBe(default);
public async Task AddAlbumAsync_Should_AddAlbumToUow() { var dto = new AlbumAddDTO { Name = "name", Description = "desc" }; mockUow.Setup(uow => uow.Albums.AddAsync(It.Is <Album>(a => a.Name == dto.Name))).Verifiable(); await service.AddAlbumAsync(dto); mockUow.Verify(); }
public async Task AddAlbumAsync_Should_SaveUow() { var dto = new AlbumAddDTO { Name = "name", Description = "desc" }; mockUow.Setup(uow => uow.Albums.AddAsync(It.IsAny <Album>())); mockUow.Setup(uow => uow.SaveAsync()).Verifiable(); await service.AddAlbumAsync(dto); mockUow.Verify(); }
//Do not forget to update userId in controller! public async Task <AlbumDTO> AddAlbumAsync(AlbumAddDTO albumDTO) { if (albumDTO == null) { throw null; } Album album = _mp.Map <Album>(albumDTO); var curDateTime = DateTime.Now; album.Created = curDateTime; album.Updated = curDateTime; await _unit.Albums.AddAsync(album); await _unit.SaveAsync(); return(_mp.Map <AlbumDTO>(album)); }