-
Notifications
You must be signed in to change notification settings - Fork 125
Home work3 entity framework #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KennyDeew
wants to merge
6
commits into
OTUS-NET:main
Choose a base branch
from
KennyDeew:HomeWork3_EntityFramework
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
14d2a80
added EfRepository implementing IRepository
3e4cf0a
added SQLite DB
2b6c19c
added CRUD for CusttomerController
b1efd91
added PreferencesController
e72e6da
implemented PromoCodesController methods
a1d08c3
added 2 migrations
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
16 changes: 16 additions & 0 deletions
16
Homeworks/EF/src/PromoCodeFactory.Core/Abstractions/Repositories/ICustomerRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
|
|
||
| Task<Customer> GetByIdWithPreferenceAsync(Guid id); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Homeworks/EF/src/PromoCodeFactory.Core/Domain/Administration/Employee.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
14 changes: 13 additions & 1 deletion
14
Homeworks/EF/src/PromoCodeFactory.Core/Domain/Administration/Role.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
|
|
||
|
|
||
| } | ||
| } |
16 changes: 15 additions & 1 deletion
16
Homeworks/EF/src/PromoCodeFactory.Core/Domain/PromoCodeManagement/Customer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
Homeworks/EF/src/PromoCodeFactory.Core/Domain/PromoCodeManagement/CustomerPreference.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
17 changes: 16 additions & 1 deletion
17
Homeworks/EF/src/PromoCodeFactory.Core/Domain/PromoCodeManagement/Preference.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
Homeworks/EF/src/PromoCodeFactory.DataAccess/Data/DataContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше возвращать IEnumerable