/// <summary> /// Async method. Gets the aggregate based on the specified key /// </summary> /// <typeparam name="TModel">The aggregate's type</typeparam> /// <param name="key">The key of the aggregate to search</param> /// <returns>The aggregate found</returns> public async Task <TModel> GetByKeyAsync <TModel>(Guid key) where TModel : class, Core.Infrastructure.IAggregateRoot { try { var model = await _context.FindAsync <TModel>(key); return(model); } catch { throw; } }
public async Task <Result <bool> > UpdateCatalogAsync(CatalogVM catalogVM) { string errSource = "UpdateCatalogAsync"; var dbCatalog = await _context.Catalogs .Include(g => g.Genre) .Include(c => c.Company) .FirstOrDefaultAsync(c => c.ID == catalogVM.ID); if (dbCatalog is null) { return(new ErrorResult <bool>( code: ErrorCode.NotFound, message: $"Could not find any Game Catalog to update. CatalogID: {catalogVM.ID}", errorSource: errSource)); } Genre dbGenre = dbCatalog.Genre; Developer dbDevloper = dbCatalog.Company; if (dbCatalog.Company.ID != catalogVM.CompanyID) // developer company changed { dbDevloper = await _context.FindAsync <Developer>(catalogVM.CompanyID); if (dbDevloper is null) { return(new ErrorResult <bool>( code: ErrorCode.NotFound, message: $"Could not find updated game developer company ID: {catalogVM.CompanyID}", errorSource: errSource)); } dbCatalog.Company = dbDevloper; } if (dbCatalog.Genre.ID != catalogVM.GenreID) // Catalog Genre changed { dbGenre = await _context.FindAsync <Genre>(catalogVM.GenreID); if (dbGenre is null) { return(new ErrorResult <bool>( code: ErrorCode.NotFound, message: $"Could not find updated game's Genre ID: {catalogVM.GenreID}", errorSource: errSource)); } dbCatalog.Genre = dbGenre; } Mapper mapper = new Mapper(new MapperConfiguration(cfg => { cfg.CreateMap <CatalogVM, Catalog>() .AfterMap((src, des) => { des.Company = dbDevloper; des.Genre = dbGenre; des.ModifyDate = DateTime.UtcNow; }); })); var x = mapper.Map <Catalog>(catalogVM); _context.Entry(dbCatalog).CurrentValues.SetValues(x); return(new Result <bool>() { Data = await _context.SaveChangesAsync() > 0 }); }
public async Task <bool> ExistAsync(Guid id) { var request = await _context.FindAsync <ClientRequest>(id); return(request != null); }