Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\PromoCodeFactory.sln (Homeworks\\EF\\src\\PromoCodeFactory.sln)",
"PreviewInSolutionExplorer": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using PromoCodeFactory.Core.Domain.PromoCodeManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PromoCodeFactory.Core.Abstractions.Repositories
{
public interface ICustomerRepository : IRepository<Customer>
{
Task<IQueryable<Customer>> GetAllWithPreferenceAsync();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше возвращать IEnumerable


Task<Customer> GetByIdWithPreferenceAsync(Guid id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ public interface IRepository<T>
Task<IEnumerable<T>> GetAllAsync();

Task<T> GetByIdAsync(Guid id);

Task<T> AddAsync(T entity);

Task UpdateAsync(T entity);

Task DeleteAsync(Guid id);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
using PromoCodeFactory.Core.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace PromoCodeFactory.Core.Domain.Administration
{
public class Employee
: BaseEntity
{
[Column("firstname"), MaxLength(50)]
public string FirstName { get; set; }

[Column("lastname"), MaxLength(50)]
public string LastName { get; set; }

[Column("fullname"), MaxLength(101)]
public string FullName => $"{FirstName} {LastName}";

[Column("email"), MaxLength(50)]
public string Email { get; set; }

[Column("mobile"), MaxLength(50)]
public string mobile { get; set; }

public Role Role { get; set; }

public Guid RoleId { get; set; }

public int AppliedPromocodesCount { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using PromoCodeFactory.Core.Domain;
using PromoCodeFactory.Core.Domain.PromoCodeManagement;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace PromoCodeFactory.Core.Domain.Administration
{
public class Role
: BaseEntity
{
[Column("name"), MaxLength(50)]
public string Name { get; set; }

[Column("description"), MaxLength(200)]
public string Description { get; set; }

/// <summary>
/// Список Employees
/// </summary>
public ICollection<Employee>? Employees { get; set; }


}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
using PromoCodeFactory.Core.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace PromoCodeFactory.Core.Domain.PromoCodeManagement
{
public class Customer
: BaseEntity
{
[Column("firstname"), MaxLength(50)]
public string FirstName { get; set; }
[Column("lastname"), MaxLength(50)]
public string LastName { get; set; }

[Column("fullname"), MaxLength(101)]
public string FullName => $"{FirstName} {LastName}";

[Column("email"), MaxLength(50)]
public string Email { get; set; }

//TODO: Списки Preferences и Promocodes
/// <summary>
/// Список CustomerPreferences
/// </summary>
public ICollection<CustomerPreference>? CustomerPreferences { get; set; }

/// <summary>
/// Список Promocodes
/// </summary>
public ICollection<PromoCode>? Promocodes { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PromoCodeFactory.Core.Domain.PromoCodeManagement
{
/// <summary>
/// связывающая таблица для Customer и Preference
/// </summary>
public class CustomerPreference
{
public Guid CustomerId { get; set; }
public Guid PreferenceId { get; set; }
public Customer Customer { get; set; }
public Preference Preference { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
namespace PromoCodeFactory.Core.Domain.PromoCodeManagement
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace PromoCodeFactory.Core.Domain.PromoCodeManagement
{
public class Preference
: BaseEntity
{
[Column("name"), MaxLength(50)]
public string Name { get; set; }

/// <summary>
/// Список CustomerPreferences
/// </summary>
public ICollection<CustomerPreference>? CustomerPreferences { get; set; }

/// <summary>
/// Список PromoCodes
/// </summary>
public ICollection<PromoCode>? PromoCodes { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Runtime;
using PromoCodeFactory.Core.Domain;
using PromoCodeFactory.Core.Domain.Administration;
Expand All @@ -8,18 +10,27 @@ namespace PromoCodeFactory.Core.Domain.PromoCodeManagement
public class PromoCode
: BaseEntity
{
[Column("code"), MaxLength(50)]
public string Code { get; set; }

[Column("serviceinfo"), MaxLength(200)]
public string ServiceInfo { get; set; }

public DateTime BeginDate { get; set; }

public DateTime EndDate { get; set; }

[Column("partnername"), MaxLength(100)]
public string PartnerName { get; set; }

public Employee PartnerManager { get; set; }

public Preference Preference { get; set; }

public Customer Customer { get; set; }

public Guid CustomerId { get; set; }

public Guid PreferenceId { get; set; }
}
}
133 changes: 133 additions & 0 deletions Homeworks/EF/src/PromoCodeFactory.DataAccess/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Microsoft.EntityFrameworkCore;
using PromoCodeFactory.Core.Domain.Administration;
using PromoCodeFactory.Core.Domain.PromoCodeManagement;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PromoCodeFactory.DataAccess.Data
{
public class DataContext : DbContext
{
public DbSet<Customer> CustomerDbSet { get; set; }
public DbSet<Preference> PreferenceDbSet { get; set; }
public DbSet<PromoCode> PromoCodeDbSet { get; set; }
public DbSet<Role> RoleDbSet { get; set; }
public DbSet<Employee> EmployeeDbSet { get; set; }

public DataContext(DbContextOptions<DataContext> options) : base(options)
{
//Database.EnsureDeleted();

var isAvalable = Database.CanConnect();
var result = isAvalable ? "Ok!" : "Fail";

Console.WriteLine($"Try tot connect: {result}");

bool isCreated = Database.EnsureCreated();
if (isCreated)
{
Console.WriteLine("db created!");
}
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseSqlite("Data Source=SQLiteTestDB.db");

//optionsBuilder
// .LogTo(Console.WriteLine)
// .EnableDetailedErrors();

//optionsBuilder.UseLazyLoadingProxies();

//base.OnConfiguring(optionsBuilder);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var adminId = Guid.Parse("53729686-a368-4eeb-8bfa-cc69b6050d02");
var partnerManagerId = Guid.Parse("b0ae7aac-5493-45cd-ad16-87426a5e7665");
var familyPreferenceId = Guid.Parse("c4bda62e-fc74-4256-a956-4760b3858cbd");
var childrenPreferenceId = Guid.Parse("76324c47-68d2-472d-abb8-33cfa8cc0c84");
var customerPetrovId = Guid.Parse("a6c8c6b1-4349-45b0-ab31-244740aaf0f0");

modelBuilder.Entity<Role>().HasData(
new Role { Id = adminId,
Name = "Admin",
Description = "Администратор",},
new Role { Id = partnerManagerId,
Name = "PartnerManager",
Description = "Партнерский менеджер" }
);

modelBuilder.Entity<Preference>().HasData(
new Preference { Id = Guid.Parse("ef7f299f-92d7-459f-896e-078ed53ef99c"),
Name = "Театр"},
new Preference { Id = familyPreferenceId,
Name = "Семья"},
new Preference { Id = childrenPreferenceId,
Name = "Дети"}
);

modelBuilder.Entity<Employee>().HasData(
new Employee { Id = Guid.Parse("451533d5-d8d5-4a11-9c7b-eb9f14e1a32f"),
Email = "[email protected]",
FirstName = "Иван",
LastName = "Сергеев",
RoleId = adminId,
AppliedPromocodesCount = 5 },
new Employee { Id = Guid.Parse("f766e2bf-340a-46ea-bff3-f1700b435895"),
Email = "[email protected]",
FirstName = "Петр",
LastName = "Андреев",
RoleId = partnerManagerId,
AppliedPromocodesCount = 10}
);

modelBuilder.Entity<Customer>().HasData(
new Customer { Id = customerPetrovId,
Email = "[email protected]",
FirstName = "Иван",
LastName = "Петров" }
);

modelBuilder.Entity<CustomerPreference>().HasData(
new CustomerPreference { CustomerId = customerPetrovId , PreferenceId = familyPreferenceId },
new CustomerPreference { CustomerId = customerPetrovId, PreferenceId = childrenPreferenceId }
);

//Связь Customer и Promocodes (1 ко многим)
modelBuilder.Entity<Customer>()
.HasMany(c => c.Promocodes)
.WithOne(p => p.Customer)
.HasForeignKey(p => p.CustomerId)
.OnDelete(DeleteBehavior.Cascade);

//Связь Customer и Preferences (много ко многим)
modelBuilder.Entity<CustomerPreference>()
.HasKey(cp => new { cp.CustomerId, cp.PreferenceId });
modelBuilder.Entity<CustomerPreference>()
.HasOne(cp => cp.Customer)
.WithMany(c => c.CustomerPreferences)
.HasForeignKey(cp => cp.CustomerId);
modelBuilder.Entity<CustomerPreference>()
.HasOne(cp => cp.Preference)
.WithMany(p => p.CustomerPreferences)
.HasForeignKey(cp => cp.PreferenceId);

//Связь Preference и PromoCodes(1 предпочтение на много промокодов)
modelBuilder.Entity<Preference>()
.HasMany(c => c.PromoCodes)
.WithOne(p => p.Preference)
.HasForeignKey(p => p.PreferenceId);

//Связь Role и Employee (1 роль на много сотрудников)
modelBuilder.Entity<Role>()
.HasMany(c => c.Employees)
.WithOne(e => e.Role)
.HasForeignKey(p => p.RoleId);
}
}
}
Loading