public AuthorDto UpdateAuthor(AuthorDto author) { Author authorEntity = Context.Author.Find(author.Id); if (authorEntity == null) { authorEntity = Context.Author.FirstOrDefault(i => i.UserId == author.UserId); } if (authorEntity == null) { throw new Exception($"Invalid author Id ({author.Id}) and UserId ({author.UserId}) ."); } authorEntity.UserId = author.UserId; authorEntity.LastName = author.LastName; authorEntity.FirstName = author.FirstName; authorEntity.Alias = author.Alias; authorEntity.Bio = author.Bio; authorEntity.Active = author.Active; authorEntity.UpdatedAt = DateTime.UtcNow; Context.SaveChanges(); author = authorEntity.Adapt <AuthorDto>(); return(author); }
public AuthorDto GetAuthor(string alias) { Author authorEntity = Context.Author.FirstOrDefault(i => i.Alias == alias); AuthorDto author = null; if (authorEntity != null) { author = authorEntity.Adapt <AuthorDto>(); } return(author); }
public AuthorDto GetAuthor(int id) { Author authorEntity = Context.Author.Find(id); if (authorEntity == null) { throw new Exception($"Invalid author Id ({id})."); } AuthorDto author = authorEntity.Adapt <AuthorDto>(); return(author); }
public async Task <bool> UpdateAsync(Author item, CancellationToken cancellationToken = default) { using (var context = new BookLibraryContext(this._optionsBuilder.Options)) { var author = await context.Authors.SingleOrDefaultAsync(o => o.Id == item.Id, cancellationToken); if (author == null) { return(false); } item.Adapt(author); await context.SaveChangesAsync(cancellationToken); return(true); } }
public AuthorDto AddAuthor(string userId, string alias, string firstName, string lastName, string bio, bool active = true) { if (string.IsNullOrWhiteSpace(userId)) { throw new ArgumentNullException("userId"); } if (string.IsNullOrWhiteSpace(alias)) { throw new ArgumentNullException("alias"); } Author entity = new Author() { UserId = userId, Alias = alias, FirstName = firstName, LastName = lastName, Bio = bio, Active = active }; Context.Author.Add(entity); Context.SaveChanges(); AuthorDto author = entity.Adapt <AuthorDto>(); return(author); }