public static void Run() { // I am showing this in code, but you would normally // do this with your DI container in your composition // root, and the instance would be created by injecting // it somewhere. var paymentStrategy = new PaymentStrategy( new IPaymentService[] { new CreditCardPayment(), // <-- inject any dependencies here new PayPalPayment() // <-- inject any dependencies here }); // Then once it is injected, you simply do this... var cc = new CreditCardModel() { CardHolderName = "Bob" }; paymentStrategy.MakePayment(cc); // Or this... var pp = new PayPalModel() { UserName = "******" }; paymentStrategy.MakePayment(pp); }
// GET: Employee public async Task <ActionResult> Index() { var employee = await _employeeRepository.GetEmployeeWithEventsAsync(CurrentUser); var todayEvent = await _employeeRepository.GetEmployeeEventByDayAsync(CurrentUser, DateTime.Now.Day, DateTime.Now.Month); var tomorrowEvent = await _employeeRepository.GetEmployeeEventByDayAsync(CurrentUser, DateTime.Now.Day + 1, DateTime.Now.Month); var strategy = new PaymentStrategy(); strategy.SetPaymentStrategy(employee.PayMethod); var payment = strategy.MakePayment(employee, DateTime.Now.Month, DateTime.Now.Year); EmployeeIndexViewModel model = new EmployeeIndexViewModel { LoggedCompany = CurrentCompany, //BaseViewModel LoggedUser = CurrentUser, //BaseViewModel LogoUrl = CurrentLogoUrl, //BaseViewModel Employee = employee, Today = todayEvent, Tomorrow = tomorrowEvent, TotalPay = payment.TotalPay, }; return(View(model)); }
/// <summary> /// Calculates company monthly payment based on month and year to be used in statistics view /// </summary> /// <param name="companyId"></param> /// <param name="month"></param> /// <param name="year"></param> /// <returns><see cref="MonthlyPaymentViewModel"/></returns> public async Task <MonthlyPaymentViewModel> MonthlyPayment(int companyId, int month, int year) { var employees = await _employeeRepository.GetEmployeesWithEventsAsync(companyId); List <Payment> payments = new List <Payment>(); PaymentStrategy strategy = new PaymentStrategy(); foreach (var e in employees) { strategy.SetPaymentStrategy(e.PayMethod); payments.Add(strategy.MakePayment(e, month, year)); } MonthlyPaymentViewModel model = new MonthlyPaymentViewModel { EmployeesNames = employees.Select(e => e.FullName).ToList(), EmployeesSalary = payments ?? new List <Payment>() }; return(model); }
public async Task <ActionResult> CreateUpdate(string username, int month, int year, string paymentId) { int currentPaymentid = int.TryParse(paymentId, out int p) ? p : 0; var user = await _employeeRepository.GetEmployeePaymentEventsAsync(username); PaymentStrategy strategy = new PaymentStrategy(); strategy.SetPaymentStrategy(user.PayMethod); var payment = strategy.MakePayment(user, month, year); payment.PaymentId = currentPaymentid; CreatePaymentViewModel model = new CreatePaymentViewModel() { LoggedUser = CurrentUser, //BaseViewModel LoggedCompany = CurrentCompany, //BaseViewModel LogoUrl = CurrentLogoUrl, //BaseViewModel Employee = user, Payment = payment }; return(View(model)); }
/// <summary> /// Method to call on configuration.cs to seed data /// </summary> /// <param name="context"></param> public static void SeedContext(this ApplicationDbContext context) { var adminStore = new UserStore <AdminUser>(context); var adminManager = new UserManager <AdminUser>(adminStore); var employeeStore = new UserStore <EmployeeUser>(context); var employeeManager = new UserManager <EmployeeUser>(employeeStore); List <IdentityRole> roles = IdentityRoleData.GetData(); Dictionary <AdminUser, string> adminUsers = AdminUserData.GetData(); List <EmployeeUser> employeeUsers = EmployeeUserData.GetData(); List <Company> companies = CompanyData.GetData(); List <EmployeeEvent> employeeEvents = new List <EmployeeEvent>(); employeeEvents.AddRange(Employee1EventData.GetData()); employeeEvents.AddRange(Employee2EventData.GetData()); employeeEvents.AddRange(Employee3EventData.GetData()); employeeEvents.AddRange(Employee4EventData.GetData()); employeeEvents.AddRange(Employee5EventData.GetData()); List <Post> posts = PostData.GetData(); List <Request> employeeRequests = RequestData.GetData(); context.Roles.AddOrUpdate(roles.ToArray()); foreach (var user in adminUsers) { adminManager.Create(user.Key, user.Value); user.Key.Roles.Add(new IdentityUserRole { RoleId = "1", UserId = user.Key.Id }); } foreach (EmployeeUser user in employeeUsers) { employeeManager.Create(user, user.UserName); user.Roles.Add(new IdentityUserRole { RoleId = "2", UserId = user.Id }); } foreach (Company company in companies) { company.Events = employeeEvents.Where(e => e.CompanyID == company.CompanyId).ToList(); company.Posts = posts.Where(c => c.CompanyId == company.CompanyId).ToList(); } foreach (EmployeeUser user in employeeUsers) { var events = employeeEvents.Where(e => e.Username == user.UserName).ToList(); var requests = employeeRequests.Where(r => r.Username == user.UserName).ToList(); user.EmployeeEvent = events; user.Requests = requests; user.Payments = new List <Payment>(); var months2020 = events.Where(e => e.Start.Value.Year == 2020).Select(e => e.Start.Value.Month).Distinct(); var months2021 = events.Where(e => e.Start.Value.Year == 2021).Select(e => e.Start.Value.Month).Distinct(); PaymentStrategy strategy = new PaymentStrategy(); strategy.SetPaymentStrategy(user.PayMethod); foreach (var month in months2020) { Payment payment = new Payment(); payment = strategy.MakePayment(user, month, 2020); user.Payments.Add(payment); context.Payments.AddOrUpdate(payment); } foreach (var month in months2021) { Payment payment = new Payment(); payment = strategy.MakePayment(user, month, 2021); user.Payments.Add(payment); context.Payments.AddOrUpdate(payment); } } context.Posts.AddOrUpdate(posts.ToArray()); context.Events.AddOrUpdate(employeeEvents.ToArray()); context.Requests.AddOrUpdate(employeeRequests.ToArray()); context.Companies.AddOrUpdate(companies.ToArray()); try { // Your code... // Could also be before try if you know the exception occurs in SaveChanges context.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }