using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using PersonalFinance.Entity;
namespace PersonalFinance
{
///
/// Create DB context for entities
///
public class EntityContext : DbContext
{
public static int x = 0;
public virtual IDbSet Payments { get; set; }
public virtual IDbSet ScheduledPayments { get; set; }
public virtual IDbSet Users { get; set; }
public EntityContext()
{
x++;
}
}
///
/// Initialization of Conxtext (temporary Alway, in production will IfModelChanges)
///
public class EntityContextInit :
DropCreateDatabaseIfModelChanges
//DropCreateDatabaseAlways
{
protected override void Seed(EntityContext context)
{
//chyba se týkala toho, že uživatel má povinný parametr login...
//dá se zjistit přes context.GetValidationErrors()
List paymentList = GetInitPaymentList();
foreach (Payment x in paymentList)
context.Payments.Add(x);
List scheduledPayment = GetInitScheduledItemList();
foreach (ScheduledPayment x in scheduledPayment)
context.ScheduledPayments.Add(x);
List userList = GetInitUserList(paymentList, scheduledPayment);
foreach (User x in userList)
context.Users.Add(x);
context.SaveChanges();
}
///
/// Predefined users (used for Moq too)
///
/// List of Usesr
public List GetInitUserList(List paymentList, List scheduledPaymentList)
{
Payment[] payment = paymentList.ToArray();
ScheduledPayment[] scheduledPayment = scheduledPaymentList.ToArray();
User a = new User("aarrgh") { UserId = 1, Login = "Tonda", Name = "Antonín", Lastname = "Neumann", Sex = Sex.Male };
a.AddPayment(payment[0]);
a.AddPayment(payment[1]);
a.AddPayment(payment[7]);
a.AddScheduledPayment(scheduledPayment[0]);
a.AddScheduledPayment(scheduledPayment[1]);
User b = new User("aarrgh") { UserId = 2, Login = "Ivetka", Name = "Iveta", Lastname = "Dobrá", Sex = Sex.Female };
b.AddPayment(payment[2]);
b.AddPayment(payment[3]);
b.AddScheduledPayment(scheduledPayment[2]);
User c = new User("aarrgh") { UserId = 3, Login = "Honzík", Name = "Jan", Lastname = "Novák", Sex = Sex.Male };
c.AddPayment(payment[4]);
User d = new User("aarrgh") { UserId = 4, Login = "Andy", Name = "Andrea", Lastname = "Nováková", Sex = Sex.Female };
d.AddPayment(payment[5]);
User e = new User("aarrgh") { UserId = 5, Login = "ĚŠČŘŽÝÁÍÉÚŮěščřžýáíéúů", Name = "John", Lastname = "Doe", Sex = Sex.Male };
e.AddPayment(payment[6]);
return new List(){a, b, c, d, e};
}
///
/// Predefined ScheduledItems (used for Moq too)
///
/// List of Payments
/// List of ScheduledItems
public List GetInitScheduledItemList()
{
return new List() {
new ScheduledPayment() { ScheduledItemId= 1, Frequency = Frequency.Monthly, LastUse = DateTime.Now,
Currency = PaymentCurrency.CZK, Price = 1700, Purpose = "Elektřina", Group = PaymentGroup.Family },
new ScheduledPayment() { ScheduledItemId= 2, Frequency = Frequency.Monthly, LastUse = DateTime.Now,
Currency = PaymentCurrency.CZK, Price = 349, Purpose = "Telefon", Group = PaymentGroup.Personal,
Until = DateTime.Now.AddDays(10), Note="Poznamka" },
new ScheduledPayment() { ScheduledItemId= 3, Frequency = Frequency.Quarterly, LastUse = DateTime.Now,
Currency = PaymentCurrency.CZK, Price = 599, Purpose = "Telefon", Group = PaymentGroup.Personal }
};
}
///
/// Predefined Payments (used for Moq too)
///
/// List of users
/// List of Paments
public List GetInitPaymentList()
{
return new List() {
new Payment(){ //0
PaymentId = 1, Currency = PaymentCurrency.CZK, Date = DateTime.Now, Price = 1700,
Purpose = "Elektřina", Group = PaymentGroup.Family, Paid = true},
new Payment(){ //1
PaymentId = 2, Currency = PaymentCurrency.EUR, Date = DateTime.Now, Price = 85,
Purpose = "Telefon (vodafone)", Group = PaymentGroup.Personal, Paid = false
},
new Payment(){ //2
PaymentId = 3, Currency = PaymentCurrency.USD, Date = DateTime.Now, Price = 150,
Purpose = "Dárek", Note = "Dárek by měl dorazit XX.YY.", Group = PaymentGroup.Private, Paid = true
},
new Payment(){ //3
PaymentId = 4, Currency = PaymentCurrency.CZK, Date = DateTime.Now, Price = 2200,
Purpose = "Elektřina", Group = PaymentGroup.Family, Paid = true
},
new Payment(){//4
PaymentId = 5, Currency = PaymentCurrency.CZK, Date = DateTime.Now, Price = 1200,
Purpose = "Povinné ručení", Group = PaymentGroup.Personal, Paid = true
},
new Payment(){//5
PaymentId = 6, Currency = PaymentCurrency.CZK, Date = DateTime.Now, Price = 1200,
Purpose = "Povinné ručení", Group = PaymentGroup.Personal, Paid = true
},
new Payment(){//6
PaymentId = 7, Currency = PaymentCurrency.CZK, Date = DateTime.Now, Price = 1200,
Purpose = "Povinné ručení", Group = PaymentGroup.Personal, Paid = true
},
new Payment(){//7
PaymentId = 8, Currency = PaymentCurrency.EUR, Date = DateTime.Now, Price = 87000,
Purpose = "Sportovní auto", Group = PaymentGroup.Private, Paid = true
}
};
}
}
}