private async Task <bool> CardHasAlreadyBeenScannedWithThisInformations(CardInfoForStudent info) { CardInfoForStudent latestScan = null; // Get the latest scan and check, if the data is the same as the scan which has been transmitted // If it is so, it is a duplicate, since the student checked his card value twice without adding or removing money. var allScansForStudent = await this.dataBase.CardInformationForStudent.Where(scan => scan.ID == info.ID) .ToListAsync(); // If there is no entrance yet, its the students first scan. if (!allScansForStudent.Any()) { return(false); } latestScan = allScansForStudent[0]; foreach (var scan in allScansForStudent) { var scannedTime = DateTime.Parse(scan.ScanDate); if (scannedTime > DateTime.Parse(latestScan.ScanDate)) { latestScan = scan; } } return(latestScan.CardValue == info.CardValue && latestScan.LastTransaction == info.LastTransaction); }
public async Task <string> PutCardInformationAsync(CardInformationModelWithPassword model) { var info = new CardInfoForStudent(); info.CardValue = model.CardValue; info.LastTransaction = model.LastTransaction; info.ID = model.ID; info.ScanDate = DateTime.Now.ToString(); if (!await this.CardHasAlreadyBeenScannedWithThisInformations(info)) { await this.dataBase.CardInformationForStudent.AddAsync(info); } await this.dataBase.SaveChangesAsync(); return("Successfully added the card information."); }
private async Task <List <CardInformationModel> > GetLatestScanForEachCard(List <CardInfoForStudent> allScannedCards) { var result = new List <CardInformationModel>(); var allIds = new List <string>(); foreach (var scan in allScannedCards) { if (!allIds.Contains(scan.ID)) { allIds.Add(scan.ID); } } foreach (var id in allIds) { CardInfoForStudent latestCardInfoForId = new CardInfoForStudent(); latestCardInfoForId.ScanDate = DateTime.MinValue.ToString(); var scansWithId = this.dataBase.CardInformationForStudent.Where(card => card.ID == id); foreach (var scan in scansWithId) { var scanDate = DateTime.Parse(scan.ScanDate); if (scanDate > DateTime.Parse(latestCardInfoForId.ScanDate)) { latestCardInfoForId = scan; } } result.Add(new CardInformationModel { ID = latestCardInfoForId.ID, CardValue = latestCardInfoForId.CardValue, LastTransaction = latestCardInfoForId.LastTransaction }); } return(result); }
public async Task <CardInformationModelForID> GetCardInformationById(string id) { var result = new CardInformationModelForID(); var allCardInformations = await this.dataBase.CardInformationForStudent.Where(cardInfo => cardInfo.ID == id).ToListAsync(); if (!allCardInformations.Any()) { throw new IDDoesNotExistException($"There is no database-entry with the ID '{id}'."); } var allValues = new List <double>(); var allLastTranscation = new List <double>(); foreach (var cardInformation in allCardInformations) { allValues.Add(cardInformation.CardValue); allLastTranscation.Add(cardInformation.LastTransaction); } result.AllCurrentValues = allValues; result.AllLastTransactions = allLastTranscation; result.CurrentValueAverage = this.GetAverage(allValues, allValues.Count); result.LastTransactionAverage = this.GetAverage(allLastTranscation, allLastTranscation.Count); CardInfoForStudent latestScan = allCardInformations[0]; foreach (var scan in allCardInformations) { var scannedTime = DateTime.Parse(scan.ScanDate); if (scannedTime > DateTime.Parse(latestScan.ScanDate)) { latestScan = scan; } } result.CurrentValue = latestScan.CardValue; result.LastTransaction = latestScan.LastTransaction; var cardStatistics = await this.GetCardStatisticsAsync(id); var scanDates = new List <string>(); var spentMoney = new List <double>(); var averagesLastTransactions = new List <double>(); var totalScanAmount = new List <int>(); foreach (var statistic in cardStatistics) { var date = statistic.Date.Replace(" 00:00:00", string.Empty).Remove(0, 3).Replace(".", " - "); scanDates.Add("Monat: " + date); spentMoney.Add(statistic.SpentMoney); averagesLastTransactions.Add(statistic.AverageLastTransaction); totalScanAmount.Add(statistic.ScanAmount); } result.ScanDates = scanDates; result.SpentMoney = spentMoney; result.AverageLastTransactions = averagesLastTransactions; result.ScanAmounts = totalScanAmount; return(result); }