public async Task CreateCarSale(CarSalesEntry carSalesEntry) { if (carSalesEntry == null) { throw new ArgumentNullException(nameof(carSalesEntry)); } var carSalesEntries = await _context.CarSalesEntries.Where(c => c.CarEntryId == carSalesEntry.CarEntryId).ToListAsync(); BlockChainHelper.VerifyBlockChain(carSalesEntries); if (carSalesEntries.Any(c => !c.IsValid)) { throw new InvalidOperationException("Block Chain was invalid"); } string previousBlockHash = null; if (carSalesEntries.Any()) { var previousCarSalesEntry = carSalesEntries.Last(); carSalesEntry.PreviousId = previousCarSalesEntry.Id; previousBlockHash = previousCarSalesEntry.Hash; } var blockText = BlockHelper.ConcatData(carSalesEntry.CarEntryId, carSalesEntry.CarNumber, carSalesEntry.Price, carSalesEntry.TransactionDate, previousBlockHash); carSalesEntry.Hash = HashHelper.Hash(blockText); _context.CarSalesEntries.Add(carSalesEntry); await _context.SaveChangesAsync(); }
public void CreateAwardeCrtificate(AwardeCrtificate awardeCrtificate) { if (awardeCrtificate == null) { throw new ArgumentNullException(nameof(awardeCrtificate)); } var awardeCrtificateEntries = _context.AwardeCrtificate.Where(c => c.CertificationId == awardeCrtificate.CertificationId).ToList(); BlockChainHelper.VerifyBlockChain(awardeCrtificateEntries); if (awardeCrtificateEntries.Any(c => !c.IsValid)) { throw new InvalidOperationException("Block Chain was invalid"); } string previousBlockHash = null; if (awardeCrtificateEntries.Any()) { var previousawardeCrtificate = awardeCrtificateEntries.Last(); awardeCrtificate.PreviousId = previousawardeCrtificate.Id; previousBlockHash = previousawardeCrtificate.Hash; } var blockText = BlockHelper.ConcatData(awardeCrtificate.CertificationId, awardeCrtificate.CrtificateNumber, awardeCrtificate.Recipient, awardeCrtificate.provider, awardeCrtificate.Notes, awardeCrtificate.Moyen, awardeCrtificate.TransactionDate, previousBlockHash); awardeCrtificate.Hash = HashHelper.Hash(blockText); _context.AwardeCrtificate.Add(awardeCrtificate); _context.SaveChanges(); }
public async Task <CarEntry> GetCar(int id) { var car = await _context.CarEntries .Include(c => c.CarSalesEntries) .SingleOrDefaultAsync(c => c.Id == id); if (car != null) { BlockChainHelper.VerifyBlockChain(car.CarSalesEntries); } return(car); }
public List <AwardeCrtificate> GetAwardeCrtificates(int id) { Certification certification = _context.Certification .Include(c => c.AwardeCrtificate) .SingleOrDefault(c => c.Id == id); if (certification != null) { BlockChainHelper.VerifyBlockChain(certification.AwardeCrtificate); } return(certification.AwardeCrtificate.ToList());// Select(a => a.AwardeCrtificate).ToList(); }
public Certification GetCertification(int id) { var certification = _context.Certification .Include(c => c.AwardeCrtificate) .SingleOrDefault(c => c.Id == id); if (certification != null) { BlockChainHelper.VerifyBlockChain(certification.AwardeCrtificate); } return(certification); }
public async Task <IActionResult> Create(TastingHistory tastingHistory) { if (ModelState.IsValid) { if (tastingHistory == null) { throw new ArgumentNullException(nameof(tastingHistory)); } var tastingHistories = await _context.TastingHistory.ToListAsync(); BlockChainHelper.VerifyBlockChain(tastingHistories); if (tastingHistories.Any(c => !c.IsValid)) { return(RedirectToAction("ErrorPage", "Home", new { message = "Something going wrong. Blockchain was corrupted!" })); } string previousBlockHash = null; if (tastingHistories.Any()) { var previousTastingHistory = tastingHistories.Last(); tastingHistory.TastingHistoryPreviousId = previousTastingHistory.TastingHistoryId; previousBlockHash = previousTastingHistory.Hash; } int tastingId = await _userAccessService.GetTastingId(); tastingHistory.TastingId = tastingId; int tasterId = await GetTasterId(); tastingHistory.TasterId = tasterId; List <int> productIds = await _context.ProductToTastings.Where(c => c.TastingId == tastingId).Select(d => d.ProductId).ToListAsync(); List <int?> productIdsTastingHistory = await _context.TastingHistory.Where(c => c.TastingId == tastingId && c.TasterId == tasterId) .Select(d => d.ProductId) .ToListAsync(); int productId = 0; if (productIds.Count > productIdsTastingHistory.Count) { int dif = productIds.Count - productIdsTastingHistory.Count; productId = productIds[productIds.Count - dif]; } if (productIds.Count == productIdsTastingHistory.Count) { return(RedirectToAction("ErrorPage", "Home", new { message = "Something going wrong. Seems you tasted all products. Tasting is ended. Thank you!" })); } tastingHistory.ProductId = productId; tastingHistory.TransactionDate = DateTime.Now; var blockText = BlockHelper.ConcatData(tastingHistory.ViewProse, tastingHistory.ViewColour, tastingHistory.BouquetClean, tastingHistory.BouquetIntensity, tastingHistory.BouquetQuality, tastingHistory.TasteColour, tastingHistory.TasteQuality, tastingHistory.TasteIntensity, tastingHistory.TasteAftertaste, tastingHistory.TastePotencial, tastingHistory.Garmony, tastingHistory.Penalty, tastingHistory.TransactionDate, previousBlockHash); tastingHistory.Hash = HashHelper.Hash(blockText); TastingHistory itemOnSave = _context.TastingHistory.Where (x => x.TastingHistoryId == tastingHistory.TastingHistoryId).FirstOrDefault(); itemOnSave = new TastingHistory() { ViewProse = tastingHistory.ViewProse, ViewColour = tastingHistory.ViewColour, BouquetClean = tastingHistory.BouquetClean, BouquetIntensity = tastingHistory.BouquetIntensity, BouquetQuality = tastingHistory.BouquetQuality, TasteColour = tastingHistory.TasteColour, TasteQuality = tastingHistory.TasteQuality, TasteIntensity = tastingHistory.TasteIntensity, TasteAftertaste = tastingHistory.TasteAftertaste, TastePotencial = tastingHistory.TastePotencial, Garmony = tastingHistory.Garmony, Penalty = tastingHistory.Penalty, TransactionDate = tastingHistory.TransactionDate, Hash = tastingHistory.Hash, TasterId = tastingHistory.TasterId, TastingId = tastingHistory.TastingId, ProductId = tastingHistory.ProductId, TastingHistoryPreviousId = tastingHistory.TastingHistoryPreviousId, }; _context.TastingHistory.Add(itemOnSave); await _context.SaveChangesAsync(); if (productIds.Count == productIdsTastingHistory.Count + 1) { return(RedirectToAction("ErrorPage", "Home", new { message = "Seems you tasted all products. Tasting is ended. Thank you!" })); } return(RedirectToAction("Create", "TastingHistories")); } return(View(tastingHistory)); }