public async Task AddAsync(Stock stock) { long nFound = await dbContext.StockCollection.CountDocumentsAsync(stockDoc => stockDoc.Code == stock.Code); if (nFound > 0) { throw new AlreadyInDbException(); } StockDocument stockDoc = mapper.Map <StockDocument>(stock); await dbContext.StockCollection.InsertOneAsync(stockDoc); }
public async Task UpdateAsync(Stock stock) { long nFound = await dbContext.StockCollection.CountDocumentsAsync(stockDoc => stockDoc.Code == stock.Code); if (nFound <= 0) { throw new NotFoundInDbException(); } StockDocument newStockDoc = mapper.Map <StockDocument>(stock); await dbContext.StockCollection.ReplaceOneAsync(stockDoc => stockDoc.Code == stock.Code, newStockDoc); }
public async Task <Stock> GetOneAsync(string code) { using (IAsyncCursor <StockDocument> query = await dbContext.StockCollection.FindAsync(stockDoc => stockDoc.Code == code)) { if (query == null) { throw new NotFoundInDbException(); } StockDocument stockDoc = query.FirstOrDefault(); if (stockDoc == null) { throw new NotFoundInDbException(); } return(mapper.Map <Stock>(stockDoc)); } }