public async Task <int> CreateNewCard() { try { TransportCard transportCard = new TransportCard { Load = 100, DateRegistered = null, CreateDate = DateTime.Now, ExpirationDate = DateTime.Now.AddYears(5), LastDateUsed = null, CardTypeId = 1 }; await _db.AddAsync(transportCard); await _db.SaveChangesAsync(); int id = transportCard.Id; return(id); } catch (Exception) { return(0); } }
public async Task <IActionResult> GetTransportCardById([FromQuery] int id) { TransportCard transportCard = await _cardService.GetTransportCardById(id); if (transportCard == null) { return(BadRequest(new { response_message = "Cannot find record with the entered transport card id. Please try again.", isError = true })); } CardInfoViewModel response = new CardInfoViewModel { CardType = _cardTypeService.GetCardTypeById(transportCard.CardTypeId).Result, CardTypeId = transportCard.CardTypeId, CreateDate = formatDateToShow(transportCard.CreateDate), DateRegistered = formatDateToShow(transportCard.DateRegistered), DiscountCardDetails = _discountCardDetailsService.GetDiscountCardDetailsById(id).Result, ExpirationDate = formatDateToShow(transportCard.ExpirationDate), Id = transportCard.Id, LastDateUsed = formatDateToShow(transportCard.LastDateUsed), Load = transportCard.Load, TodayCardUsage = _transportService.GetAllTransportById(id).Result.Count }; //result.CardType = await _cardTypeService.GetCardTypeById(result.CardTypeId); return(Ok(response)); }
public async Task <IActionResult> TopUpTransportCard([FromBody] TopUpCardViewModel tuc) { if (tuc.Amount < 100 || tuc.Amount > 10000) { return(BadRequest(new { response_message = "Cannot Proceed. You can only top up between the amount of 100 to 10000 pesos.", isError = true })); } TransportCard transportCard = await _cardService.GetTransportCardById(tuc.TransportCardId); transportCard.Load += tuc.Amount; bool response = await _cardService.TopUpAccount(transportCard); if (response) { tuc.Balance = transportCard.Load; tuc.Change = tuc.Cash - tuc.Amount; return(Ok(new { response_message = "Successfully Credited to account.", isError = false, tuc })); } else { return(BadRequest(new { response_message = "Internal Server Error. Please contact your local IT.", isError = true })); } }
// // Generators // private void GenerateUsersAndCards(int howMany = 1) { string[] names = { "Jan", "Andrzej", "Piotr", "Krzysztof", "Stanisław", "Tomasz", "Paweł", "Józef", "Marcin", "Marek", "Michał", "Grzegorz", "Jerzy", "Tadeusz", "Adam", "Łukasz", "Zbigniew", "Ryszard", "Dariusz", "Henryk", "Mariusz", "Kazimierz", "Wojciech", "Robert", "Mateusz", "Marian", "Rafał", "Jacek", "Janusz", "Mirosław", "Maciej", "Sławomir", "Jarosław", "Kamil", "Wiesław", "Roman", "Władysław", "Jakub", "Artur", "Zdzisław", "Edward", "Mieczysław", "Damian", "Dawid", "Przemysław", "Sebastian", "Czesław", "Leszek", "Daniel", "Waldemar", "Anna", "Maria", "Katarzyna", "Małgorzata", "Agnieszka", "Krystyna", "Barbara", "Ewa", "Elżbieta", "Zofia", "Janina", "Teresa", "Joanna", "Magdalena", "Monika", "Jadwiga", "Danuta", "Irena", "Halina", "Helena", "Beata", "Aleksandra", "Marta", "Dorota", "Marianna", "Grażyna", "Jolanta", "Stanisława", "Iwona", "Karolina", "Bożena", "Urszula", "Justyna", "Renata", "Alicja", "Paulina", "Sylwia", "Natalia", "Wanda", "Agata", "Aneta", "Izabela", "Ewelina", "Marzena", "Wiesława", "Genowefa", "Patrycja", "Kazimiera", "Edyta", "Stefania" }; string[] lastNames = { "Nowak", "Kowalski", "Wiśniewski", "Dąbrowski", "Lewandowski", "Wójcik", "Kamiński", "Kowalczyk", "Zieliński", "Szymański", "Woźniak", "Kozłowski", "Jankowski", "Wojciechowski", "Kwiatkowski", "Kaczmarek", "Mazur", "Krawczyk", "Piotrowski", "Grabowski", "Nowakowski", "Pawłowski", "Michalski", "Nowicki", "Adamczyk", "Dudek", "Zając", "Wieczorek", "Jabłoński", "Król", "Majewski", "Olszewski", "Jaworski", "Wróbel", "Malinowski", "Pawlak", "Witkowski", "Walczak", "Stępień", "Górski", "Rutkowski", "Michalak", "Sikora", "Ostrowski", "Baran", "Duda", "Szewczyk", "Tomaszewski", "Pietrzak", "Marciniak", "Wróblewski", "Zalewski", "Jakubowski", "Jasiński", "Zawadzki", "Sadowski", "Bąk", "Chmielewski", "Włodarczyk", "Borkowski", "Czarnecki", "Sawicki", "Sokołowski", "Urbański", "Kubiak", "Maciejewski", "Szczepański", "Kucharski", "Wilk", "Kalinowski", "Lis", "Mazurek", "Wysocki", "Adamski", "Kaźmierczak", "Wasilewski", "Sobczak", "Czerwiński", "Andrzejewski", "Cieślak", "Głowacki", "Zakrzewski", "Kołodziej", "Sikorski", "Krajewski", "Gajewski", "Szymczak", "Szulc", "Baranowski", "Laskowski", "Brzeziński", "Makowski", "Ziółkowski", "Przybylski" }; for (int i = 0; i < howMany; i++) { string randomName = names[random.Next(names.Length)]; string randomLastName = lastNames[random.Next(lastNames.Length)]; string randomId = RandomString(10, "0123456789"); var user = new User() { Name = randomName, Surname = randomLastName, PersonalId = randomId }; Array values = Enum.GetValues(typeof(CardType)); CardType randomCardType = (CardType)values.GetValue(random.Next(values.Length)); bool randomIsActive = (random.Next(2) == 0); var card = new TransportCard() { CardType = randomCardType, IsActive = randomIsActive, User = user }; context.Users.Add(user); context.TransportCards.Add(card); } }
private void GenerateCardWithRandomUser() { var users = context.Users.ToArray(); var randomUser = (User)users[random.Next(users.Length)]; var card = new TransportCard() { CardType = CardType.YoungInhabitant, IsActive = true, User = randomUser }; context.TransportCards.Add(card); }
public async Task <IActionResult> DiscountCardRegistration([FromBody] DiscountCardRegistrationViewModel dcr) { TransportCard transportCard = await _cardService.GetTransportCardById(dcr.TransportCardId); if ((DateTime.Now.Date - transportCard.CreateDate.Date).Days > 60) { return(BadRequest(new { response_message = "Cannot Proceed. Card is only eligible for discount registration within 60 days after the card was purchased.", isError = true })); } DiscountCardDetails toSave = new DiscountCardDetails { TransportCardId = dcr.TransportCardId, GovernmentIdNumber = dcr.IdNumber, GovernmentIdType = dcr.IdType }; CardType cardType = await _cardTypeService.GetCardTypeById(dcr.CardTypeId); bool discountDetailsResult = await _discountCardDetailsService.AddDiscountCardDetails(toSave); if (!discountDetailsResult) { HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return(BadRequest(new { response_message = "Internal Server Error. Please contact your local IT.", isError = true })); } bool transportCardUpdateResult = await _cardService.DiscountCardRegistration(dcr.TransportCardId, cardType.id); //rolling back / deleting inserted if (!transportCardUpdateResult) { DiscountCardDetails discountCardDetails = await _discountCardDetailsService.GetDiscountCardDetailsById(cardType.id); bool responseRollback = await _discountCardDetailsService.DeleteDiscountCardDetails(discountCardDetails); //while (!responseRollback) //{ // await Task.Delay(3000); // responseRollback = _discountCardDetailsService.DeleteDiscountCardDetails(discountCardDetails).GetAwaiter().GetResult(); //} //transportCardUpdateResult = true; } if (transportCardUpdateResult) { return(Ok(new { response_message = "Successfully registered discount card.", isError = false })); } return(BadRequest(new { response_message = "Internal Server Error. Please contact your local IT.", isError = true })); }
public ActionResult Save(TransportCard cards) { if (ModelState.IsValid) { try { _context.TransportCards.Add(cards); _context.SaveChanges(); } catch (Exception ex) { } } return(View("TransactionSuccess")); }
public async Task <bool> UpdateCard(TransportCard transportCard) { try { _db.Update(transportCard); await _db.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public async Task <bool> DiscountCardRegistration(int transportTypeId, int cardTypeId) { try { TransportCard item = await GetTransportCardById(transportTypeId); item.CardTypeId = cardTypeId; item.DateRegistered = DateTime.Now; _db.Update(item); await _db.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public async Task <ActionResult> CreateCard(RegisterViewModel model) { if (ModelState.IsValid) { var user = new TransportCard { UserName = model.Email, Email = "*****@*****.**", TransportCardRoleId = 1, LastUsedDate = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult ApplyDiscounted(TransportCard card) { var transportCard = _context.TransportCards .SingleOrDefault(c => c.TransportCardUniqueId == card.TransportCardUniqueId); if (transportCard != null) { if (transportCard.CardTypeId != 2) { transportCard.CardTypeId = 2; transportCard.Identification = card.Identification; transportCard.LastUsed = DateTime.Now; _context.SaveChanges(); } else { return(View("TransactionError")); } } return(View("TransactionSuccess")); }
public async Task <int> PurchaseTransportCard(int load) { if (load < _initialLoadAmount || load > _maxLoadAmount) { throw new Exception($"Initial load must be {_initialLoadAmount} or higher but less than {_maxLoadAmount}"); } var transportCard = new TransportCard { Load = load }; _context.Add(transportCard); if (await _context.SaveChangesAsync() <= 0) { throw new Exception("Purchasing card failed"); } return(transportCard.Id); }
public async Task <IActionResult> ProcessTrip([FromBody] ProcessTransportViewModel pt) { Transport transport = new Transport { Cost = pt.Cost - (pt.Discount + pt.DailyDiscount), MrtLine = pt.MrtLine, TransportCardId = pt.TransportCardId, TrasportDate = DateTime.Now }; bool resultAddTransport = await _transportService.AddTransport(transport); TransportCard transportCard = await _cardService.GetTransportCardById(pt.TransportCardId); transportCard.Load -= pt.Cost; transportCard.LastDateUsed = DateTime.Now; transportCard.ExpirationDate = DateTime.Now.AddYears(5); bool resultUpdateCard = await _cardService.UpdateCard(transportCard); return(Ok(new { response_message = "Successfuly proccessed trip." })); }
public async Task <TransportCard> GetTransportCardById(int id) { TransportCard toReturn = await _db.TransportCard.FindAsync(id); return(toReturn); }
public TransportCardService() { TransportCard = new TransportCard(); TransportCard.Balance = 0; }