From 0feeef6d197d2bbefbdcaba296e159959bd1f697 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:25:37 +0100 Subject: [PATCH 01/69] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 58b28a7..99c7da5 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ ASP.NET Core 6/7 MVC & EF (Code-First) (HttpGet/Post/Put/Delete) Swagger Azure A ******************************************************** STEPS (Swagger Azure AD Autht - Swagger Azure AD Login)= ******************************************************** +![Authorized Page](https://user-images.githubusercontent.com/57094137/210284591-4a4d7eaa-275f-49ea-bf0a-a87bdf80f35f.jpg) Opening programs = -------------------------------------- @@ -185,4 +186,5 @@ GO BACK TO Running program, Swagger page= - RUN Methods (get/post/put/update/delete) - NOW WE HAVE ACCESS TO DATA, GETTING RESPONSE, AND METHODS WORKING !!!!! :)) +![Got Token](https://user-images.githubusercontent.com/57094137/210284617-5a4dd58f-d2f8-4c34-9fa5-ba21c21aa178.jpg) ******************************************************************************* From d4e1d76e3920e108f970d0cd950c7c3f1aa7ef4a Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:39:35 +0100 Subject: [PATCH 02/69] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 99c7da5..dfebb80 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,14 @@ Making changes on project using data from Azure Portal = - this data which are "Application (client) id, Directory (tenant) id etc..." is created automatically when creating SwaggerAzureADApi and uploaded to appsettings.json. +![SwaggerAzureADApi App regs](https://user-images.githubusercontent.com/57094137/210285097-b0dc9055-def3-4d04-a382-feeca68fd0c0.jpg) RUN project whether it is working or not = ------------------------------------------ - run get method and execute, - if you get response as "Status Code 401", it is okay and we need to make some changes in Program.cs. +![Error 401](https://user-images.githubusercontent.com/57094137/210284937-710c53ce-bfda-4620-b65b-b09d291b90a0.jpg) Making changes on Program.cs/StudentApp = ----------------------------------------- @@ -120,6 +122,8 @@ Making changes on project/StudentApp = -------------------------------------- - go to appsettings.json and add these codes from program.cs following "AzureAd" : +![VS App regs](https://user-images.githubusercontent.com/57094137/210285156-39e8a2ef-de27-4434-a763-d8705256eaf6.jpg) + "AzureAd": { "Instance": "https://login.microsoftonline.com/", "Domain": "11august22.onmicrosoft.com", From 53af0b9f238d2373e8f5c103094a681378493d0e Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:42:37 +0100 Subject: [PATCH 03/69] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dfebb80..c0ad9ae 100644 --- a/README.md +++ b/README.md @@ -186,9 +186,13 @@ GO BACK TO Running program, Swagger page= - on the new opened page Sign in with your account (account logging information in which you created SwaggerAzureADApi) - on the new page give permission and click "Accept", - now WE GOT TOKEN. + +![Got Token](https://user-images.githubusercontent.com/57094137/210284617-5a4dd58f-d2f8-4c34-9fa5-ba21c21aa178.jpg) + - TURN TO running program, swagger page, - RUN Methods (get/post/put/update/delete) - NOW WE HAVE ACCESS TO DATA, GETTING RESPONSE, AND METHODS WORKING !!!!! :)) -![Got Token](https://user-images.githubusercontent.com/57094137/210284617-5a4dd58f-d2f8-4c34-9fa5-ba21c21aa178.jpg) +![Success 200](https://user-images.githubusercontent.com/57094137/210285257-fdc3b299-1be5-4d9f-9274-0072d637664e.jpg) + ******************************************************************************* From 712b7140233bb01954da2853a58c53670c3df198 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:50:27 +0100 Subject: [PATCH 04/69] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c0ad9ae..844cf8c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ Creating Swagger Azure AD Api = -------------------------------------- Now we are in project/api in Visual Studio/-Code, +![ConnectedServices-Microsoft Identity Platform](https://user-images.githubusercontent.com/57094137/210285547-d47ebdbb-f02a-488c-8fec-3328709199b9.jpg) + - On Connected services/Solution Explorer click right & add Microsoft Identity Platform, - On the new opened page if you are not login to Azure login/sign in to Azure, - On the new opened page after logging in you will see your created App's name as a list, @@ -65,6 +67,8 @@ Making changes on Program.cs/StudentApp = ----------------------------------------- - go to "builder.Services.AddSwaggerGen();" and add these codes to add OpenApiInfo & OpenApiSecurityScheme: +![AddSwaggerGen](https://user-images.githubusercontent.com/57094137/210285353-71bc8838-238f-4d49-8c1f-901f82e43c4b.jpg) + builder.Services.AddSwaggerGen( c=> { From 6133a07ae7126f4fec443c4237f93ba41ec0f11a Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:52:54 +0100 Subject: [PATCH 05/69] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 844cf8c..ad15cae 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,10 @@ Creating Swagger Azure AD Api = -------------------------------------- Now we are in project/api in Visual Studio/-Code, +- On Connected services/Solution Explorer click right & add Microsoft Identity Platform, + ![ConnectedServices-Microsoft Identity Platform](https://user-images.githubusercontent.com/57094137/210285547-d47ebdbb-f02a-488c-8fec-3328709199b9.jpg) -- On Connected services/Solution Explorer click right & add Microsoft Identity Platform, - On the new opened page if you are not login to Azure login/sign in to Azure, - On the new opened page after logging in you will see your created App's name as a list, - We are still on this page, on the upper right click (+) to create/register application, From e71559b91ec18cb53a628fda6b14df04f8e63393 Mon Sep 17 00:00:00 2001 From: Omer Kurtuldu Date: Wed, 4 Jan 2023 21:43:11 +0100 Subject: [PATCH 06/69] Add validation for students adress field --- .../ValidateNotNullOrEmptyAttribute.cs | 16 ++++++++++++++++ StudentApp/Models/Students.cs | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs diff --git a/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs b/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs new file mode 100644 index 0000000..a52c6a2 --- /dev/null +++ b/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using StudentApp.Models; + +namespace StudentApp.Controllers.Validations; + +public class ValidateNotNullOrEmptyAttribute : ValidationAttribute +{ + public new string ErrorMessage = "Adress can not be null or empty"; + protected override ValidationResult IsValid(object? value, ValidationContext validationContext) + { + var model = (Students)validationContext.ObjectInstance; + if(model.Address is null || model.Address.Equals(string.Empty)) + return new ValidationResult(ErrorMessage); + return ValidationResult.Success; + } +} \ No newline at end of file diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index 89e7471..1aa2c36 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -1,4 +1,6 @@  +using StudentApp.Controllers.Validations; + namespace StudentApp.Models { public class Students @@ -10,7 +12,7 @@ public class Students public string? FirstName { get; set; } public string? LastName { get; set; } - public string? Address { get; set; } + [ValidateNotNullOrEmptyAttribute] public string? Address { get; set; } public string? TlfNo { get; set; } public string? School { get; set; } From cadad3b36fc1dfff75e891a7a6d7b87a5ec15591 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Fri, 6 Jan 2023 14:42:06 +0100 Subject: [PATCH 07/69] Commits 06.jan.23 --- .../Validations/EmploymentMgmtContainer.cs | 8 +++++ .../ValidateNotNullOrEmptyAttribute.cs | 35 ++++++++++++++++--- StudentApp/Data/StudentAppContext.cs | 1 - .../Migrations/20221201142435_initdata.cs | 4 ++- .../StudentAppContextModelSnapshot.cs | 6 ++++ StudentApp/Models/AddStudentRequest.cs | 24 ++++++++----- StudentApp/Models/Students.cs | 6 ++-- StudentApp/Models/UpdateStudentRequest.cs | 23 +++++++----- StudentApp/Program.cs | 3 ++ StudentApp/Repo/Repo.cs | 2 ++ StudentApp/StudentApp.csproj | 6 +++- 11 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs diff --git a/StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs b/StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs new file mode 100644 index 0000000..767bf97 --- /dev/null +++ b/StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs @@ -0,0 +1,8 @@ +namespace StudentApp.Controllers.Validations +{ + internal class EmploymentMgmtContainer + { + public object EmploymentMgmtEndDate { get; internal set; } + public object EmploymentMgmtStartDate { get; internal set; } + } +} \ No newline at end of file diff --git a/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs b/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs index a52c6a2..f0132a1 100644 --- a/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs +++ b/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs @@ -1,16 +1,43 @@ + using System.ComponentModel.DataAnnotations; using StudentApp.Models; namespace StudentApp.Controllers.Validations; +// Null check public class ValidateNotNullOrEmptyAttribute : ValidationAttribute { - public new string ErrorMessage = "Adress can not be null or empty"; + public new string? ErrorMessage; + protected override ValidationResult IsValid(object? value, ValidationContext validationContext) + { + var model = (AddStudentRequest)validationContext.ObjectInstance; + ErrorMessage = $"{model} can not be null or empty"; + if ((model.UserName is null || model.UserName.Equals(string.Empty)) || + (model.FirstName is null || model.FirstName.Equals(string.Empty)) || + (model.SecondName is null || model.SecondName.Equals(string.Empty)) || + (model.LastName is null || model.LastName.Equals(string.Empty)) || + (model.Address is null || model.Address.Equals(string.Empty)) || + (model.TlfNo is null || model.TlfNo.Equals(string.Empty)) || + (model.School is null || model.School.Equals(string.Empty))) + return new ValidationResult(ErrorMessage); + + return ValidationResult.Success; + } +} + + +public class ValidJoinDate : ValidationAttribute +{ + public new string ErrorMessage = "Join date can not be smaller than current date"; protected override ValidationResult IsValid(object? value, ValidationContext validationContext) { - var model = (Students)validationContext.ObjectInstance; - if(model.Address is null || model.Address.Equals(string.Empty)) + DateTime _dateJoin = Convert.ToDateTime(value); + + if (_dateJoin < DateTime.Now) + { return new ValidationResult(ErrorMessage); + } return ValidationResult.Success; } -} \ No newline at end of file +} + diff --git a/StudentApp/Data/StudentAppContext.cs b/StudentApp/Data/StudentAppContext.cs index 36dda08..fd6c0f6 100644 --- a/StudentApp/Data/StudentAppContext.cs +++ b/StudentApp/Data/StudentAppContext.cs @@ -1,6 +1,5 @@  using Microsoft.EntityFrameworkCore; -using StudentApp.Models; namespace StudentApp.Data { diff --git a/StudentApp/Migrations/20221201142435_initdata.cs b/StudentApp/Migrations/20221201142435_initdata.cs index 47d3196..1f657e8 100644 --- a/StudentApp/Migrations/20221201142435_initdata.cs +++ b/StudentApp/Migrations/20221201142435_initdata.cs @@ -18,10 +18,12 @@ protected override void Up(MigrationBuilder migrationBuilder) .Annotation("SqlServer:Identity", "1, 1"), UserName = table.Column(type: "nvarchar(max)", nullable: true), FirstName = table.Column(type: "nvarchar(max)", nullable: true), + SecondName = table.Column(type: "nvarchar(max)", nullable: true), LastName = table.Column(type: "nvarchar(max)", nullable: true), Address = table.Column(type: "nvarchar(max)", nullable: true), TlfNo = table.Column(type: "nvarchar(max)", nullable: true), - School = table.Column(type: "nvarchar(max)", nullable: true) + School = table.Column(type: "nvarchar(max)", nullable: true), + RegistrationDate = table.Column(type: "datetime", nullable: false) }, constraints: table => { diff --git a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs index 6c81d52..23f0d80 100644 --- a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs +++ b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs @@ -33,6 +33,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("FirstName") .HasColumnType("nvarchar(max)"); + b.Property("SecondName") + .HasColumnType("nvarchar(max)"); + b.Property("LastName") .HasColumnType("nvarchar(max)"); @@ -45,6 +48,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("UserName") .HasColumnType("nvarchar(max)"); + b.Property("RegistrationDate") + .HasColumnType("datetime"); + b.HasKey("Id"); b.ToTable("Student"); diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index 0266882..76fbca0 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -1,4 +1,6 @@ -namespace StudentApp.Models +using StudentApp.Controllers.Validations; + +namespace StudentApp.Models { public class AddStudentRequest { @@ -9,19 +11,23 @@ public AddStudentRequest(Students students) { UserName= students.UserName; FirstName= students.FirstName; + SecondName = students.SecondName; LastName= students.LastName; Address= students.Address; TlfNo= students.TlfNo; School= students.School; + RegistrationDate = students.RegistrationDate; } - public string? UserName { get; set; } - public string? FirstName { get; set; } - public string? LastName { get; set; } + [ValidateNotNullOrEmpty] public string? UserName { get; set; } + [ValidateNotNullOrEmpty] public string? FirstName { get; set; } + [ValidateNotNullOrEmpty] public string? SecondName { get; set; } + [ValidateNotNullOrEmpty] public string? LastName { get; set; } - public string? Address { get; set; } - public string? TlfNo { get; set; } - public string? School { get; set; } + [ValidateNotNullOrEmpty] public string? Address { get; set; } + [ValidateNotNullOrEmpty] public string? TlfNo { get; set; } + [ValidateNotNullOrEmpty] public string? School { get; set; } + [ValidJoinDate] public DateTime RegistrationDate { get; internal set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { @@ -29,10 +35,12 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) { UserName = addStudentRequest.UserName, FirstName = addStudentRequest.FirstName, + SecondName = addStudentRequest.SecondName, LastName = addStudentRequest.LastName, Address = addStudentRequest.Address, TlfNo = addStudentRequest.TlfNo, - School = addStudentRequest.School + School = addStudentRequest.School, + RegistrationDate= addStudentRequest.RegistrationDate, }; } } diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index 1aa2c36..5d387a3 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -1,5 +1,4 @@  -using StudentApp.Controllers.Validations; namespace StudentApp.Models { @@ -10,11 +9,12 @@ public class Students public int Id { get; set; } public string? UserName { get; set; } public string? FirstName { get; set; } + public string? SecondName { get; set; } public string? LastName { get; set; } - [ValidateNotNullOrEmptyAttribute] public string? Address { get; set; } + public string? Address { get; set; } public string? TlfNo { get; set; } public string? School { get; set; } - + public DateTime RegistrationDate { get; internal set; } } } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index a0c90f5..c99988c 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -1,4 +1,6 @@  +using StudentApp.Controllers.Validations; + namespace StudentApp.Models { public class UpdateStudentRequest { @@ -9,19 +11,22 @@ public UpdateStudentRequest(Students students) { UserName = students?.UserName??"Test"; FirstName = students?.FirstName; + SecondName =students?.SecondName; LastName = students?.LastName; Address = students?.Address; TlfNo = students?.TlfNo; School = students?.School; + RegistrationDate = students.RegistrationDate; } - public string? UserName { get; set; } - public string? FirstName { get; set; } - public string? LastName { get; set; } - - public string? Address { get; set; } - public string? TlfNo { get; set; } - public string? School { get; set; } + [ValidateNotNullOrEmpty] public string? UserName { get; set; } + [ValidateNotNullOrEmpty] public string? FirstName { get; set; } + [ValidateNotNullOrEmpty] public string? SecondName { get; set; } + [ValidateNotNullOrEmpty] public string? LastName { get; set; } + [ValidateNotNullOrEmpty] public string? Address { get; set; } + [ValidateNotNullOrEmpty] public string? TlfNo { get; set; } + [ValidateNotNullOrEmpty] public string? School { get; set; } + [ValidJoinDate] public DateTime RegistrationDate { get; internal set; } public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) { @@ -29,10 +34,12 @@ public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) { UserName = updateStudentRequest.UserName, FirstName = updateStudentRequest.FirstName, + SecondName = updateStudentRequest.SecondName, LastName = updateStudentRequest.LastName, Address = updateStudentRequest.Address, TlfNo = updateStudentRequest.TlfNo, - School = updateStudentRequest.School + School = updateStudentRequest.School, + RegistrationDate = updateStudentRequest.RegistrationDate }; } } diff --git a/StudentApp/Program.cs b/StudentApp/Program.cs index 9100215..491ee24 100644 --- a/StudentApp/Program.cs +++ b/StudentApp/Program.cs @@ -18,6 +18,8 @@ // Add services to the container. builder.Services.AddControllersWithViews(); builder.Services.AddEndpointsApiExplorer(); + +#region SecureApi builder.Services.AddSwaggerGen( c=> { @@ -51,6 +53,7 @@ } }); }); +#endregion // Add interfaces (Context) builder.Services.AddTransient(); diff --git a/StudentApp/Repo/Repo.cs b/StudentApp/Repo/Repo.cs index ab77f0e..d764795 100644 --- a/StudentApp/Repo/Repo.cs +++ b/StudentApp/Repo/Repo.cs @@ -34,10 +34,12 @@ public async Task UpdateStudent(int id, Students students) { result.UserName = students.UserName; result.FirstName = students.FirstName; + result.SecondName = students.SecondName; result.LastName = students.LastName; result.TlfNo = students.TlfNo; result.School = students.School; result.Address = students.Address; + result.RegistrationDate= students.RegistrationDate; _context.Student.Update(result); await _context.SaveChangesAsync(); diff --git a/StudentApp/StudentApp.csproj b/StudentApp/StudentApp.csproj index 644aaf5..dcf4f4d 100644 --- a/StudentApp/StudentApp.csproj +++ b/StudentApp/StudentApp.csproj @@ -9,8 +9,10 @@ + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -23,12 +25,14 @@ + - + + From 8901b3bd66e4a4f4230f0711c1b76ef9436b1df5 Mon Sep 17 00:00:00 2001 From: Omer Kurtuldu Date: Tue, 10 Jan 2023 21:39:26 +0100 Subject: [PATCH 08/69] Update validation --- .../Validations/IsNotNullOrEmptyAttribute.cs | 8 ++++++++ StudentApp/Models/AddStudentRequest.cs | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs diff --git a/StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs b/StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs new file mode 100644 index 0000000..e8889a7 --- /dev/null +++ b/StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs @@ -0,0 +1,8 @@ +using System.ComponentModel.DataAnnotations; + +namespace StudentApp.Controllers.Validations; + +public class IsNotNullOrEmptyAttribute : ValidationAttribute +{ + public override bool IsValid(object? value) => value is not null && !value.Equals(string.Empty); +} \ No newline at end of file diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index 76fbca0..51b380f 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -1,4 +1,5 @@ -using StudentApp.Controllers.Validations; +using System.ComponentModel.DataAnnotations; +using StudentApp.Controllers.Validations; namespace StudentApp.Models { @@ -19,15 +20,15 @@ public AddStudentRequest(Students students) RegistrationDate = students.RegistrationDate; } - [ValidateNotNullOrEmpty] public string? UserName { get; set; } - [ValidateNotNullOrEmpty] public string? FirstName { get; set; } - [ValidateNotNullOrEmpty] public string? SecondName { get; set; } - [ValidateNotNullOrEmpty] public string? LastName { get; set; } + [RequiredAttribute] [IsValid] public string UserName { get; set; } + [RequiredAttribute] [IsValid] public string FirstName { get; set; } + public string? SecondName { get; set; } + [RequiredAttribute] [IsValid] public string LastName { get; set; } - [ValidateNotNullOrEmpty] public string? Address { get; set; } - [ValidateNotNullOrEmpty] public string? TlfNo { get; set; } - [ValidateNotNullOrEmpty] public string? School { get; set; } - [ValidJoinDate] public DateTime RegistrationDate { get; internal set; } + [RequiredAttribute] [IsValid] public string Address { get; set; } + [RequiredAttribute] [IsValid] public string TlfNo { get; set; } + [RequiredAttribute] [IsValid] public string School { get; set; } + [RequiredAttribute] [ValidJoinDate] public DateTime RegistrationDate { get; set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { From ffc8235833afdf2cc6207c1d097eff88a88221b7 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Tue, 10 Jan 2023 23:33:09 +0100 Subject: [PATCH 09/69] validations update --- .../Validations/EmploymentMgmtContainer.cs | 8 ---- .../Validations/IsNotNullOrEmptyAttribute.cs | 1 + .../Validations/ValidateJoinDateAttribute.cs | 33 ++++++++++++++ .../ValidateNotNullOrEmptyAttribute.cs | 43 ------------------- StudentApp/Models/AddStudentRequest.cs | 20 ++++----- StudentApp/Models/Students.cs | 17 ++++---- StudentApp/Models/UpdateStudentRequest.cs | 30 ++++++------- StudentApp/StudentApp.csproj | 2 +- 8 files changed, 69 insertions(+), 85 deletions(-) delete mode 100644 StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs create mode 100644 StudentApp/Controllers/Validations/ValidateJoinDateAttribute.cs delete mode 100644 StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs diff --git a/StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs b/StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs deleted file mode 100644 index 767bf97..0000000 --- a/StudentApp/Controllers/Validations/EmploymentMgmtContainer.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace StudentApp.Controllers.Validations -{ - internal class EmploymentMgmtContainer - { - public object EmploymentMgmtEndDate { get; internal set; } - public object EmploymentMgmtStartDate { get; internal set; } - } -} \ No newline at end of file diff --git a/StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs b/StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs index e8889a7..6f760f3 100644 --- a/StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs +++ b/StudentApp/Controllers/Validations/IsNotNullOrEmptyAttribute.cs @@ -1,3 +1,4 @@ + using System.ComponentModel.DataAnnotations; namespace StudentApp.Controllers.Validations; diff --git a/StudentApp/Controllers/Validations/ValidateJoinDateAttribute.cs b/StudentApp/Controllers/Validations/ValidateJoinDateAttribute.cs new file mode 100644 index 0000000..35ad0e2 --- /dev/null +++ b/StudentApp/Controllers/Validations/ValidateJoinDateAttribute.cs @@ -0,0 +1,33 @@ + +using System.ComponentModel.DataAnnotations; +using StudentApp.Models; + +namespace StudentApp.Controllers.Validations; + + +public class ValidateAddJoinDateAttribute : ValidationAttribute +{ + public new string ErrorMessage = "Registration date can not be greater than current date"; + protected override ValidationResult IsValid(object? value, ValidationContext validationContext) + { + var model = (AddStudentRequest)validationContext.ObjectInstance; + + if (model.RegistrationDate < DateTime.UtcNow) return ValidationResult.Success; + + return new ValidationResult(ErrorMessage); + } +} + +public class ValidateUpdateJoinDateAttribute : ValidationAttribute +{ + public new string ErrorMessage = "Registration date can not be greater than current date"; + protected override ValidationResult IsValid(object? value, ValidationContext validationContext) + { + var model = (UpdateStudentRequest)validationContext.ObjectInstance; + + if (model.RegistrationDate < DateTime.UtcNow) return ValidationResult.Success; + + return new ValidationResult(ErrorMessage); + } +} + diff --git a/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs b/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs deleted file mode 100644 index f0132a1..0000000 --- a/StudentApp/Controllers/Validations/ValidateNotNullOrEmptyAttribute.cs +++ /dev/null @@ -1,43 +0,0 @@ - -using System.ComponentModel.DataAnnotations; -using StudentApp.Models; - -namespace StudentApp.Controllers.Validations; - -// Null check -public class ValidateNotNullOrEmptyAttribute : ValidationAttribute -{ - public new string? ErrorMessage; - protected override ValidationResult IsValid(object? value, ValidationContext validationContext) - { - var model = (AddStudentRequest)validationContext.ObjectInstance; - ErrorMessage = $"{model} can not be null or empty"; - if ((model.UserName is null || model.UserName.Equals(string.Empty)) || - (model.FirstName is null || model.FirstName.Equals(string.Empty)) || - (model.SecondName is null || model.SecondName.Equals(string.Empty)) || - (model.LastName is null || model.LastName.Equals(string.Empty)) || - (model.Address is null || model.Address.Equals(string.Empty)) || - (model.TlfNo is null || model.TlfNo.Equals(string.Empty)) || - (model.School is null || model.School.Equals(string.Empty))) - return new ValidationResult(ErrorMessage); - - return ValidationResult.Success; - } -} - - -public class ValidJoinDate : ValidationAttribute -{ - public new string ErrorMessage = "Join date can not be smaller than current date"; - protected override ValidationResult IsValid(object? value, ValidationContext validationContext) - { - DateTime _dateJoin = Convert.ToDateTime(value); - - if (_dateJoin < DateTime.Now) - { - return new ValidationResult(ErrorMessage); - } - return ValidationResult.Success; - } -} - diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index 51b380f..2bf01b5 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -1,4 +1,4 @@ -using System.ComponentModel.DataAnnotations; + using StudentApp.Controllers.Validations; namespace StudentApp.Models @@ -10,7 +10,7 @@ public AddStudentRequest() } public AddStudentRequest(Students students) { - UserName= students.UserName; + UserName= students.UserName??"Test"; FirstName= students.FirstName; SecondName = students.SecondName; LastName= students.LastName; @@ -20,15 +20,15 @@ public AddStudentRequest(Students students) RegistrationDate = students.RegistrationDate; } - [RequiredAttribute] [IsValid] public string UserName { get; set; } - [RequiredAttribute] [IsValid] public string FirstName { get; set; } - public string? SecondName { get; set; } - [RequiredAttribute] [IsValid] public string LastName { get; set; } + [IsNotNullOrEmpty] public string UserName { get; set; } + [IsNotNullOrEmpty] public string FirstName { get; set; } + public string SecondName { get; set; } + [IsNotNullOrEmpty] public string LastName { get; set; } - [RequiredAttribute] [IsValid] public string Address { get; set; } - [RequiredAttribute] [IsValid] public string TlfNo { get; set; } - [RequiredAttribute] [IsValid] public string School { get; set; } - [RequiredAttribute] [ValidJoinDate] public DateTime RegistrationDate { get; set; } + [IsNotNullOrEmpty] public string Address { get; set; } + [IsNotNullOrEmpty] public string TlfNo { get; set; } + [IsNotNullOrEmpty] public string School { get; set; } + [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index 5d387a3..64ab93f 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -1,4 +1,5 @@  +using System.ComponentModel.DataAnnotations; namespace StudentApp.Models { @@ -7,14 +8,14 @@ public class Students internal int id; public int Id { get; set; } - public string? UserName { get; set; } - public string? FirstName { get; set; } - public string? SecondName { get; set; } - public string? LastName { get; set; } + [Required] public string UserName { get; set; } + [Required] public string FirstName { get; set; } + public string SecondName { get; set; } + [Required] public string LastName { get; set; } - public string? Address { get; set; } - public string? TlfNo { get; set; } - public string? School { get; set; } - public DateTime RegistrationDate { get; internal set; } + [Required] public string Address { get; set; } + [Required] public string TlfNo { get; set; } + [Required] public string School { get; set; } + [Required] public DateTime RegistrationDate { get; internal set; } } } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index c99988c..31b816c 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -9,24 +9,24 @@ public UpdateStudentRequest() } public UpdateStudentRequest(Students students) { - UserName = students?.UserName??"Test"; - FirstName = students?.FirstName; - SecondName =students?.SecondName; - LastName = students?.LastName; - Address = students?.Address; - TlfNo = students?.TlfNo; - School = students?.School; + UserName = students.UserName??"Test"; + FirstName = students.FirstName; + SecondName =students.SecondName; + LastName = students.LastName; + Address = students.Address; + TlfNo = students.TlfNo; + School = students.School; RegistrationDate = students.RegistrationDate; } - [ValidateNotNullOrEmpty] public string? UserName { get; set; } - [ValidateNotNullOrEmpty] public string? FirstName { get; set; } - [ValidateNotNullOrEmpty] public string? SecondName { get; set; } - [ValidateNotNullOrEmpty] public string? LastName { get; set; } - [ValidateNotNullOrEmpty] public string? Address { get; set; } - [ValidateNotNullOrEmpty] public string? TlfNo { get; set; } - [ValidateNotNullOrEmpty] public string? School { get; set; } - [ValidJoinDate] public DateTime RegistrationDate { get; internal set; } + [IsNotNullOrEmpty] public string UserName { get; set; } + [IsNotNullOrEmpty] public string FirstName { get; set; } + public string SecondName { get; set; } + [IsNotNullOrEmpty] public string LastName { get; set; } + [IsNotNullOrEmpty] public string Address { get; set; } + [IsNotNullOrEmpty] public string TlfNo { get; set; } + [IsNotNullOrEmpty] public string School { get; set; } + [ValidateUpdateJoinDate] public DateTime RegistrationDate { get; set; } public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) { diff --git a/StudentApp/StudentApp.csproj b/StudentApp/StudentApp.csproj index dcf4f4d..3632e6f 100644 --- a/StudentApp/StudentApp.csproj +++ b/StudentApp/StudentApp.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0 enable enable 36532767-0a66-4c60-8c8d-f74116c147fc From 5c690020bf7d8e4be1f5f126dedc21df81c965af Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Thu, 12 Jan 2023 13:35:31 +0100 Subject: [PATCH 10/69] commit message 12.jan. --- StudentApp/Data/StudentAppContext.cs | 32 +++++++++++++++++-- .../Migrations/20221201142435_initdata.cs | 3 +- .../StudentAppContextModelSnapshot.cs | 3 +- StudentApp/Models/AddStudentRequest.cs | 7 ++-- StudentApp/Models/Students.cs | 17 ++++++++-- StudentApp/Models/UpdateStudentRequest.cs | 9 +++--- StudentApp/Repo/Repo.cs | 14 ++++---- 7 files changed, 63 insertions(+), 22 deletions(-) diff --git a/StudentApp/Data/StudentAppContext.cs b/StudentApp/Data/StudentAppContext.cs index fd6c0f6..f0fad44 100644 --- a/StudentApp/Data/StudentAppContext.cs +++ b/StudentApp/Data/StudentAppContext.cs @@ -1,5 +1,6 @@  using Microsoft.EntityFrameworkCore; +using StudentApp.Models; namespace StudentApp.Data { @@ -8,13 +9,38 @@ public class StudentAppContext : DbContext public StudentAppContext() { } - public StudentAppContext(DbContextOptions options) : base(options) { } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=StudentApp.Data;Trusted_Connection=True"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasOne(s => s.Address) + .WithOne(ad => ad.Students) + .HasForeignKey(ad => ad.AddressOfStudentId); + } + + public DbSet Students { get; set; } + public DbSet StudentAddresses { get; set; } + + + //public StudentAppContext(DbSet students, DbSet studentAddresses) + //{ + // Students = students; + // StudentAddresses = studentAddresses; + //} + //DbContextOptions dbContextOptions { get; set; } - public DbSet Student { get; set; } - public object Students { get; set; } + ////public studentappcontext() + //public studentappcontext(dbcontextoptions options) + // : base(options) { } + //public dbset student { get; set; } + //public object students { get; set; } } } diff --git a/StudentApp/Migrations/20221201142435_initdata.cs b/StudentApp/Migrations/20221201142435_initdata.cs index 1f657e8..0fb4bd2 100644 --- a/StudentApp/Migrations/20221201142435_initdata.cs +++ b/StudentApp/Migrations/20221201142435_initdata.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore.Migrations; +using StudentApp.Models; #nullable disable @@ -20,7 +21,7 @@ protected override void Up(MigrationBuilder migrationBuilder) FirstName = table.Column(type: "nvarchar(max)", nullable: true), SecondName = table.Column(type: "nvarchar(max)", nullable: true), LastName = table.Column(type: "nvarchar(max)", nullable: true), - Address = table.Column(type: "nvarchar(max)", nullable: true), + Address = table.Column(type: "nvarchar(max)", nullable: true), TlfNo = table.Column(type: "nvarchar(max)", nullable: true), School = table.Column(type: "nvarchar(max)", nullable: true), RegistrationDate = table.Column(type: "datetime", nullable: false) diff --git a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs index 23f0d80..0de1cb4 100644 --- a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs +++ b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using StudentApp.Data; +using StudentApp.Models; #nullable disable @@ -27,7 +28,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - b.Property("Address") + b.Property("Address") .HasColumnType("nvarchar(max)"); b.Property("FirstName") diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index 2bf01b5..dae92b6 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -14,21 +14,20 @@ public AddStudentRequest(Students students) FirstName= students.FirstName; SecondName = students.SecondName; LastName= students.LastName; - Address= students.Address; TlfNo= students.TlfNo; School= students.School; RegistrationDate = students.RegistrationDate; + Address = students.Address; } [IsNotNullOrEmpty] public string UserName { get; set; } [IsNotNullOrEmpty] public string FirstName { get; set; } public string SecondName { get; set; } [IsNotNullOrEmpty] public string LastName { get; set; } - - [IsNotNullOrEmpty] public string Address { get; set; } [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } + [IsNotNullOrEmpty] public StudentAddress Address{ get; set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { @@ -38,10 +37,10 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) FirstName = addStudentRequest.FirstName, SecondName = addStudentRequest.SecondName, LastName = addStudentRequest.LastName, - Address = addStudentRequest.Address, TlfNo = addStudentRequest.TlfNo, School = addStudentRequest.School, RegistrationDate= addStudentRequest.RegistrationDate, + Address = addStudentRequest.Address }; } } diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index 64ab93f..a300efa 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -6,16 +6,29 @@ namespace StudentApp.Models public class Students { internal int id; + private readonly Students students; public int Id { get; set; } [Required] public string UserName { get; set; } [Required] public string FirstName { get; set; } public string SecondName { get; set; } [Required] public string LastName { get; set; } - - [Required] public string Address { get; set; } [Required] public string TlfNo { get; set; } [Required] public string School { get; set; } [Required] public DateTime RegistrationDate { get; internal set; } + + [Required] public StudentAddress Address { get; set; } + } + + public class StudentAddress + { + [Required] public int StudentAddressId { get; set; } + [Required] public string address { get; set; } + [Required] public string City { get; set; } + [Required] public int PostNumber { get; set; } + [Required] public string Country { get; set; } + [Required] public int AddressOfStudentId { get; set; } + [Required] public Students Students { get; set; } + } } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index 31b816c..abbbe14 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -13,20 +13,21 @@ public UpdateStudentRequest(Students students) FirstName = students.FirstName; SecondName =students.SecondName; LastName = students.LastName; - Address = students.Address; TlfNo = students.TlfNo; School = students.School; RegistrationDate = students.RegistrationDate; + Address = students.Address; } [IsNotNullOrEmpty] public string UserName { get; set; } [IsNotNullOrEmpty] public string FirstName { get; set; } public string SecondName { get; set; } [IsNotNullOrEmpty] public string LastName { get; set; } - [IsNotNullOrEmpty] public string Address { get; set; } [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateUpdateJoinDate] public DateTime RegistrationDate { get; set; } + [IsNotNullOrEmpty] public StudentAddress Address { get; set; } + public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) { @@ -36,10 +37,10 @@ public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) FirstName = updateStudentRequest.FirstName, SecondName = updateStudentRequest.SecondName, LastName = updateStudentRequest.LastName, - Address = updateStudentRequest.Address, TlfNo = updateStudentRequest.TlfNo, School = updateStudentRequest.School, - RegistrationDate = updateStudentRequest.RegistrationDate + RegistrationDate = updateStudentRequest.RegistrationDate, + Address = updateStudentRequest.Address }; } } diff --git a/StudentApp/Repo/Repo.cs b/StudentApp/Repo/Repo.cs index d764795..3f3974e 100644 --- a/StudentApp/Repo/Repo.cs +++ b/StudentApp/Repo/Repo.cs @@ -15,21 +15,21 @@ public Repo(StudentAppContext context) } public Task> Get() => - _context.Student.ToListAsync(); + _context.Students.ToListAsync(); public async Task GetAsId(int id) => - await _context.Student.FirstOrDefaultAsync(s => s.Id == id); + await _context.Students.FirstOrDefaultAsync(s => s.Id == id); public async Task AddStudent(Students students) { - await _context.Student.AddAsync(students); + await _context.Students.AddAsync(students); await _context.SaveChangesAsync(); return students; } public async Task UpdateStudent(int id, Students students) { - var result = await _context.Student.FirstOrDefaultAsync(s => s.Id == id); + var result = await _context.Students.FirstOrDefaultAsync(s => s.Id == id); if (result != null) { result.UserName = students.UserName; @@ -41,7 +41,7 @@ public async Task UpdateStudent(int id, Students students) result.Address = students.Address; result.RegistrationDate= students.RegistrationDate; - _context.Student.Update(result); + _context.Students.Update(result); await _context.SaveChangesAsync(); return result; } @@ -50,14 +50,14 @@ public async Task UpdateStudent(int id, Students students) public async Task DeleteStudent(int id) { - var result = await _context.Student.FindAsync(id); + var result = await _context.Students.FindAsync(id); if (result == null) { return null; } try { - _context.Student.Remove(result); + _context.Students.Remove(result); await _context.SaveChangesAsync(); return true; } From e662eb9708e6567e58672962776cddc3c1a7294e Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Sat, 14 Jan 2023 00:20:32 +0100 Subject: [PATCH 11/69] AddressModel --- StudentApp/Data/StudentAppContext.cs | 20 +-- .../20221201142435_initdata.Designer.cs | 60 --------- .../Migrations/20221201142435_initdata.cs | 42 ------ .../20230113204620_InitialDb.Designer.cs | 121 ++++++++++++++++++ .../Migrations/20230113204620_InitialDb.cs | 73 +++++++++++ .../StudentAppContextModelSnapshot.cs | 72 +++++++++-- StudentApp/Models/AddStudentRequest.cs | 18 ++- StudentApp/Models/AddressStudentRequest.cs | 38 ++++++ StudentApp/Models/Students.cs | 30 ++--- StudentApp/Models/UpdateStudentRequest.cs | 24 +++- StudentApp/Program.cs | 15 ++- StudentApp/Repo/Repo.cs | 18 +-- StudentApp/StudentApp.csproj | 4 + StudentApp/appsettings.json | 2 +- 14 files changed, 374 insertions(+), 163 deletions(-) delete mode 100644 StudentApp/Migrations/20221201142435_initdata.Designer.cs delete mode 100644 StudentApp/Migrations/20221201142435_initdata.cs create mode 100644 StudentApp/Migrations/20230113204620_InitialDb.Designer.cs create mode 100644 StudentApp/Migrations/20230113204620_InitialDb.cs create mode 100644 StudentApp/Models/AddressStudentRequest.cs diff --git a/StudentApp/Data/StudentAppContext.cs b/StudentApp/Data/StudentAppContext.cs index f0fad44..f701251 100644 --- a/StudentApp/Data/StudentAppContext.cs +++ b/StudentApp/Data/StudentAppContext.cs @@ -23,24 +23,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity() .HasOne(s => s.Address) .WithOne(ad => ad.Students) - .HasForeignKey(ad => ad.AddressOfStudentId); + .HasForeignKey(ad => ad.StudentsId); } - public DbSet Students { get; set; } - public DbSet StudentAddresses { get; set; } - - - //public StudentAppContext(DbSet students, DbSet studentAddresses) - //{ - // Students = students; - // StudentAddresses = studentAddresses; - //} - //DbContextOptions dbContextOptions { get; set; } - - ////public studentappcontext() - //public studentappcontext(dbcontextoptions options) - // : base(options) { } - //public dbset student { get; set; } - //public object students { get; set; } + public DbSet Student { get; set; } + public DbSet StudentAddress { get; set; } } } diff --git a/StudentApp/Migrations/20221201142435_initdata.Designer.cs b/StudentApp/Migrations/20221201142435_initdata.Designer.cs deleted file mode 100644 index f70ed60..0000000 --- a/StudentApp/Migrations/20221201142435_initdata.Designer.cs +++ /dev/null @@ -1,60 +0,0 @@ -// -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using StudentApp.Data; - -#nullable disable - -namespace StudentApp.Migrations -{ - [DbContext(typeof(StudentAppContext))] - [Migration("20221201142435_initdata")] - partial class initdata - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.0") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("StudentApp.Models.Student", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Address") - .HasColumnType("nvarchar(max)"); - - b.Property("FirstName") - .HasColumnType("nvarchar(max)"); - - b.Property("LastName") - .HasColumnType("nvarchar(max)"); - - b.Property("School") - .HasColumnType("nvarchar(max)"); - - b.Property("TlfNo") - .HasColumnType("nvarchar(max)"); - - b.Property("UserName") - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("Student"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/StudentApp/Migrations/20221201142435_initdata.cs b/StudentApp/Migrations/20221201142435_initdata.cs deleted file mode 100644 index 0fb4bd2..0000000 --- a/StudentApp/Migrations/20221201142435_initdata.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; -using StudentApp.Models; - -#nullable disable - -namespace StudentApp.Migrations -{ - /// - public partial class initdata : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Student", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - UserName = table.Column(type: "nvarchar(max)", nullable: true), - FirstName = table.Column(type: "nvarchar(max)", nullable: true), - SecondName = table.Column(type: "nvarchar(max)", nullable: true), - LastName = table.Column(type: "nvarchar(max)", nullable: true), - Address = table.Column(type: "nvarchar(max)", nullable: true), - TlfNo = table.Column(type: "nvarchar(max)", nullable: true), - School = table.Column(type: "nvarchar(max)", nullable: true), - RegistrationDate = table.Column(type: "datetime", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Student", x => x.Id); - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Student"); - } - } -} diff --git a/StudentApp/Migrations/20230113204620_InitialDb.Designer.cs b/StudentApp/Migrations/20230113204620_InitialDb.Designer.cs new file mode 100644 index 0000000..5c72426 --- /dev/null +++ b/StudentApp/Migrations/20230113204620_InitialDb.Designer.cs @@ -0,0 +1,121 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using StudentApp.Data; + +#nullable disable + +namespace StudentApp.Migrations +{ + [DbContext(typeof(StudentAppContext))] + [Migration("20230113204620_InitialDb")] + partial class InitialDb + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("StudentApp.Models.StudentAddress", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("City") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Country") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PostNumber") + .HasColumnType("int"); + + b.Property("StudentsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("StudentsId") + .IsUnique(); + + b.ToTable("StudentAddress"); + }); + + modelBuilder.Entity("StudentApp.Models.Students", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RegistrationDate") + .HasColumnType("datetime2"); + + b.Property("School") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SecondName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("TlfNo") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Student"); + }); + + modelBuilder.Entity("StudentApp.Models.StudentAddress", b => + { + b.HasOne("StudentApp.Models.Students", "Students") + .WithOne("Address") + .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Students"); + }); + + modelBuilder.Entity("StudentApp.Models.Students", b => + { + b.Navigation("Address") + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/StudentApp/Migrations/20230113204620_InitialDb.cs b/StudentApp/Migrations/20230113204620_InitialDb.cs new file mode 100644 index 0000000..22e05cf --- /dev/null +++ b/StudentApp/Migrations/20230113204620_InitialDb.cs @@ -0,0 +1,73 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace StudentApp.Migrations +{ + /// + public partial class InitialDb : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Student", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserName = table.Column(type: "nvarchar(max)", nullable: false), + FirstName = table.Column(type: "nvarchar(max)", nullable: false), + SecondName = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false), + TlfNo = table.Column(type: "nvarchar(max)", nullable: false), + School = table.Column(type: "nvarchar(max)", nullable: false), + RegistrationDate = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Student", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "StudentAddress", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Address = table.Column(type: "nvarchar(max)", nullable: false), + City = table.Column(type: "nvarchar(max)", nullable: false), + PostNumber = table.Column(type: "int", nullable: false), + Country = table.Column(type: "nvarchar(max)", nullable: false), + StudentsId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_StudentAddress", x => x.Id); + table.ForeignKey( + name: "FK_StudentAddress_Student_StudentsId", + column: x => x.StudentsId, + principalTable: "Student", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_StudentAddress_StudentsId", + table: "StudentAddress", + column: "StudentsId", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "StudentAddress"); + + migrationBuilder.DropTable( + name: "Student"); + } + } +} diff --git a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs index 0de1cb4..222fe81 100644 --- a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs +++ b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs @@ -1,8 +1,10 @@ // +using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using StudentApp.Data; -using StudentApp.Models; #nullable disable @@ -20,7 +22,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - modelBuilder.Entity("StudentApp.Models.Student", b => + modelBuilder.Entity("StudentApp.Models.StudentAddress", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -28,34 +30,88 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - b.Property("Address") + b.Property("Address") + .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("FirstName") + b.Property("City") + .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("SecondName") + b.Property("Country") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PostNumber") + .HasColumnType("int"); + + b.Property("StudentsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("StudentsId") + .IsUnique(); + + b.ToTable("StudentAddress"); + }); + + modelBuilder.Entity("StudentApp.Models.Students", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("FirstName") + .IsRequired() .HasColumnType("nvarchar(max)"); b.Property("LastName") + .IsRequired() .HasColumnType("nvarchar(max)"); + b.Property("RegistrationDate") + .HasColumnType("datetime2"); + b.Property("School") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SecondName") + .IsRequired() .HasColumnType("nvarchar(max)"); b.Property("TlfNo") + .IsRequired() .HasColumnType("nvarchar(max)"); b.Property("UserName") + .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("RegistrationDate") - .HasColumnType("datetime"); - b.HasKey("Id"); b.ToTable("Student"); }); + + modelBuilder.Entity("StudentApp.Models.StudentAddress", b => + { + b.HasOne("StudentApp.Models.Students", "Students") + .WithOne("Address") + .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Students"); + }); + + modelBuilder.Entity("StudentApp.Models.Students", b => + { + b.Navigation("Address") + .IsRequired(); + }); #pragma warning restore 612, 618 } } diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index dae92b6..1ebefee 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -17,7 +17,10 @@ public AddStudentRequest(Students students) TlfNo= students.TlfNo; School= students.School; RegistrationDate = students.RegistrationDate; - Address = students.Address; + Address = students.Address.Address; + City = students.Address.City; + PostNumber = students.Address.PostNumber; + Country= students.Address.Country; } [IsNotNullOrEmpty] public string UserName { get; set; } @@ -27,7 +30,10 @@ public AddStudentRequest(Students students) [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public StudentAddress Address{ get; set; } + [IsNotNullOrEmpty] public string Address{ get; set; } + [IsNotNullOrEmpty] public string City { get; set; } + [IsNotNullOrEmpty] public int PostNumber { get; set; } + [IsNotNullOrEmpty] public string Country { get; set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { @@ -40,7 +46,13 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) TlfNo = addStudentRequest.TlfNo, School = addStudentRequest.School, RegistrationDate= addStudentRequest.RegistrationDate, - Address = addStudentRequest.Address + Address = new StudentAddress + { + Address= addStudentRequest.Address, + City = addStudentRequest.City, + PostNumber = addStudentRequest.PostNumber, + Country = addStudentRequest.Country + } }; } } diff --git a/StudentApp/Models/AddressStudentRequest.cs b/StudentApp/Models/AddressStudentRequest.cs new file mode 100644 index 0000000..5b1586e --- /dev/null +++ b/StudentApp/Models/AddressStudentRequest.cs @@ -0,0 +1,38 @@ + +using StudentApp.Controllers.Validations; + +namespace StudentApp.Models +{ + public class AddressStudentRequest + { + public AddressStudentRequest() + { + } + public AddressStudentRequest(Students students) + { + Address = students.Address.Address; + City = students.Address.City; + PostNumber = students.Address.PostNumber; + Country= students.Address.Country; + } + + [IsNotNullOrEmpty] public string Address{ get; set; } + [IsNotNullOrEmpty] public string City { get; set; } + [IsNotNullOrEmpty] public int PostNumber { get; set; } + [IsNotNullOrEmpty] public string Country { get; set; } + + public virtual Students AddressStudent(AddressStudentRequest addressStudentRequest) + { + return new Students + { + Address = new StudentAddress + { + Address= addressStudentRequest.Address, + City = addressStudentRequest.City, + PostNumber = addressStudentRequest.PostNumber, + Country = addressStudentRequest.Country + } + }; + } + } +} diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index a300efa..0f19139 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -6,29 +6,29 @@ namespace StudentApp.Models public class Students { internal int id; - private readonly Students students; + private readonly Students Student; public int Id { get; set; } - [Required] public string UserName { get; set; } - [Required] public string FirstName { get; set; } + public string UserName { get; set; } + public string FirstName { get; set; } public string SecondName { get; set; } - [Required] public string LastName { get; set; } - [Required] public string TlfNo { get; set; } - [Required] public string School { get; set; } - [Required] public DateTime RegistrationDate { get; internal set; } + public string LastName { get; set; } + public string TlfNo { get; set; } + public string School { get; set; } + public DateTime RegistrationDate { get; internal set; } - [Required] public StudentAddress Address { get; set; } + public StudentAddress Address { get; set; } } public class StudentAddress { - [Required] public int StudentAddressId { get; set; } - [Required] public string address { get; set; } - [Required] public string City { get; set; } - [Required] public int PostNumber { get; set; } - [Required] public string Country { get; set; } - [Required] public int AddressOfStudentId { get; set; } - [Required] public Students Students { get; set; } + public int Id { get; set; } + public string Address { get; set; } + public string City { get; set; } + public int PostNumber { get; set; } + public string Country { get; set; } + public int StudentsId { get; set; } + public Students Students { get; set; } } } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index abbbe14..3881d38 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -9,14 +9,17 @@ public UpdateStudentRequest() } public UpdateStudentRequest(Students students) { - UserName = students.UserName??"Test"; + UserName = students.UserName ?? "Test"; FirstName = students.FirstName; - SecondName =students.SecondName; + SecondName = students.SecondName; LastName = students.LastName; TlfNo = students.TlfNo; School = students.School; RegistrationDate = students.RegistrationDate; - Address = students.Address; + Address = students.Address.Address; + City = students.Address.City; + PostNumber = students.Address.PostNumber; + Country = students.Address.Country; } [IsNotNullOrEmpty] public string UserName { get; set; } @@ -25,8 +28,11 @@ public UpdateStudentRequest(Students students) [IsNotNullOrEmpty] public string LastName { get; set; } [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } - [ValidateUpdateJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public StudentAddress Address { get; set; } + [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } + [IsNotNullOrEmpty] public string Address { get; set; } + [IsNotNullOrEmpty] public string City { get; set; } + [IsNotNullOrEmpty] public int PostNumber { get; set; } + [IsNotNullOrEmpty] public string Country { get; set; } public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) @@ -40,7 +46,13 @@ public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) TlfNo = updateStudentRequest.TlfNo, School = updateStudentRequest.School, RegistrationDate = updateStudentRequest.RegistrationDate, - Address = updateStudentRequest.Address + Address = new StudentAddress + { + Address = updateStudentRequest.Address, + City = updateStudentRequest.City, + PostNumber = updateStudentRequest.PostNumber, + Country = updateStudentRequest.Country + } }; } } diff --git a/StudentApp/Program.cs b/StudentApp/Program.cs index 491ee24..51b0099 100644 --- a/StudentApp/Program.cs +++ b/StudentApp/Program.cs @@ -1,5 +1,4 @@ - using Microsoft.EntityFrameworkCore; using StudentApp.Data; using StudentApp.Repo; @@ -7,17 +6,27 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Identity.Web; using Microsoft.OpenApi.Models; +using System.Text.Json.Serialization; var builder = WebApplication.CreateBuilder(args); +builder.Services.AddControllers(); builder.Services.AddDbContext(option => option.UseSqlServer(builder.Configuration.GetConnectionString("StudentAppContext") ?? throw new InvalidOperationException("Connection string 'StudentAppContext' not found."))); - builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")); -// Add services to the container. +#region AddingServicesToTheContainer builder.Services.AddControllersWithViews(); builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddControllers() + .AddJsonOptions(options => + { + options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; + options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; + options.JsonSerializerOptions.AllowTrailingCommas = true; + options.JsonSerializerOptions.PropertyNamingPolicy = null; + }); +#endregion #region SecureApi builder.Services.AddSwaggerGen( diff --git a/StudentApp/Repo/Repo.cs b/StudentApp/Repo/Repo.cs index 3f3974e..850b66b 100644 --- a/StudentApp/Repo/Repo.cs +++ b/StudentApp/Repo/Repo.cs @@ -1,7 +1,9 @@  +using Microsoft.Azure.Amqp.Framing; using Microsoft.EntityFrameworkCore; using StudentApp.Data; using StudentApp.Models; +using System.Diagnostics.Metrics; namespace StudentApp.Repo; @@ -15,21 +17,21 @@ public Repo(StudentAppContext context) } public Task> Get() => - _context.Students.ToListAsync(); + _context.Student.ToListAsync(); public async Task GetAsId(int id) => - await _context.Students.FirstOrDefaultAsync(s => s.Id == id); + await _context.Student.FirstOrDefaultAsync(s => s.Id == id); public async Task AddStudent(Students students) { - await _context.Students.AddAsync(students); + await _context.Student.AddAsync(students); await _context.SaveChangesAsync(); return students; } public async Task UpdateStudent(int id, Students students) { - var result = await _context.Students.FirstOrDefaultAsync(s => s.Id == id); + var result = await _context.Student.FirstOrDefaultAsync(s => s.Id == id); if (result != null) { result.UserName = students.UserName; @@ -38,10 +40,10 @@ public async Task UpdateStudent(int id, Students students) result.LastName = students.LastName; result.TlfNo = students.TlfNo; result.School = students.School; - result.Address = students.Address; result.RegistrationDate= students.RegistrationDate; + result.Address = students.Address; - _context.Students.Update(result); + _context.Student.Update(result); await _context.SaveChangesAsync(); return result; } @@ -50,14 +52,14 @@ public async Task UpdateStudent(int id, Students students) public async Task DeleteStudent(int id) { - var result = await _context.Students.FindAsync(id); + var result = await _context.Student.FindAsync(id); if (result == null) { return null; } try { - _context.Students.Remove(result); + _context.Student.Remove(result); await _context.SaveChangesAsync(); return true; } diff --git a/StudentApp/StudentApp.csproj b/StudentApp/StudentApp.csproj index 3632e6f..be96ced 100644 --- a/StudentApp/StudentApp.csproj +++ b/StudentApp/StudentApp.csproj @@ -35,4 +35,8 @@ + + + + diff --git a/StudentApp/appsettings.json b/StudentApp/appsettings.json index 5fb5c37..d861b90 100644 --- a/StudentApp/appsettings.json +++ b/StudentApp/appsettings.json @@ -7,7 +7,7 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "StudentAppContext": "Server=(localdb)\\mssqllocaldb;Database=StudentApp.Data;Trusted_Connection=True;Connect Timeout=30;MultipleActiveResultSets=true" + "StudentAppContext": "Server=(localdb)\\MSSQLLocalDB;Database=StudentApp;Trusted_Connection=True;Connect Timeout=30;MultipleActiveResultSets=true" }, "AzureAd": { "Instance": "https://login.microsoftonline.com/", From ee0345241fdc21a6ecff94de2f2dd0f76b7442f9 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Wed, 25 Jan 2023 12:15:40 +0100 Subject: [PATCH 12/69] commit message --- .editorconfig | 86 +++++++++++++ StudentApp-1.0.sln | 7 +- StudentApp/Controllers/StudentsController.cs | 27 ++-- StudentApp/Data/StudentAppContext.cs | 12 +- .../Migrations/20230113204620_InitialDb.cs | 2 +- ...0116125527_StudentAppMigration.Designer.cs | 121 ++++++++++++++++++ .../20230116125527_StudentAppMigration.cs | 22 ++++ .../StudentAppContextModelSnapshot.cs | 10 +- StudentApp/Models/AddStudentRequest.cs | 22 +--- StudentApp/Models/AddressStudentRequest.cs | 17 +-- StudentApp/Models/StudentResponse.cs | 12 ++ StudentApp/Models/Students.cs | 8 +- StudentApp/Models/UpdateStudentRequest.cs | 22 +--- StudentApp/Program.cs | 3 +- StudentApp/Repo/Repo.cs | 24 +--- StudentApp/StudentApp.csproj | 6 +- StudentApp/Views/Students/Create.cshtml | 6 +- StudentApp/Views/Students/Delete.cshtml | 4 +- StudentApp/Views/Students/Details.cshtml | 4 +- StudentApp/Views/Students/Edit.cshtml | 6 +- StudentApp/Views/Students/Index.cshtml | 4 +- 21 files changed, 319 insertions(+), 106 deletions(-) create mode 100644 .editorconfig create mode 100644 StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs create mode 100644 StudentApp/Migrations/20230116125527_StudentAppMigration.cs create mode 100644 StudentApp/Models/StudentResponse.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7c94183 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,86 @@ +[*.cs] + +# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. +dotnet_diagnostic.CS8618.severity = none +csharp_using_directive_placement = outside_namespace:silent +csharp_prefer_simple_using_statement = true:suggestion +csharp_prefer_braces = true:silent +csharp_style_namespace_declarations = block_scoped:silent +csharp_style_prefer_method_group_conversion = true:silent +csharp_style_prefer_top_level_statements = true:silent +csharp_style_expression_bodied_methods = false:silent +csharp_style_expression_bodied_constructors = false:silent +csharp_style_expression_bodied_operators = false:silent +csharp_style_expression_bodied_properties = true:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_accessors = true:silent +csharp_style_expression_bodied_lambdas = true:silent +csharp_style_expression_bodied_local_functions = false:silent +csharp_indent_labels = one_less_than_current +csharp_space_around_binary_operators = before_and_after + +[*.{cs,vb}] +#### Naming styles #### + +# Naming rules + +dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion +dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface +dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i + +dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.types_should_be_pascal_case.symbols = types +dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case + +dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members +dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case + +# Symbol specifications + +dotnet_naming_symbols.interface.applicable_kinds = interface +dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.interface.required_modifiers = + +dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum +dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.types.required_modifiers = + +dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method +dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.non_field_members.required_modifiers = + +# Naming styles + +dotnet_naming_style.begins_with_i.required_prefix = I +dotnet_naming_style.begins_with_i.required_suffix = +dotnet_naming_style.begins_with_i.word_separator = +dotnet_naming_style.begins_with_i.capitalization = pascal_case + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_prefer_simplified_boolean_expressions = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_compound_assignment = true:suggestion +dotnet_style_prefer_simplified_interpolation = true:suggestion +dotnet_style_operator_placement_when_wrapping = beginning_of_line +tab_width = 4 +indent_size = 4 +end_of_line = crlf diff --git a/StudentApp-1.0.sln b/StudentApp-1.0.sln index 28d9b62..15b963f 100644 --- a/StudentApp-1.0.sln +++ b/StudentApp-1.0.sln @@ -3,7 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33110.190 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StudentApp", "StudentApp\StudentApp.csproj", "{B42F1925-CAD9-41DE-877E-ECFDE1FB29FA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StudentApp", "StudentApp\StudentApp.csproj", "{B42F1925-CAD9-41DE-877E-ECFDE1FB29FA}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0BF9E2E4-6976-4061-AF81-649048F33541}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/StudentApp/Controllers/StudentsController.cs b/StudentApp/Controllers/StudentsController.cs index 271fb4f..d55220f 100644 --- a/StudentApp/Controllers/StudentsController.cs +++ b/StudentApp/Controllers/StudentsController.cs @@ -21,41 +21,46 @@ public StudentsController(IService service) } [HttpGet] - public async Task> GetStudent() + public async Task>> GetStudent() { + var students = new List(); var result = await _service.Get(); - return Ok(result); + if (result is null) return Ok(students); + for (var i = 0; i < result.Count;i++) + { + students.Add(new StudentResponse(result[i])); + } + return Ok(students); } [HttpGet("GetAsId/")] - public async Task> GetAsId(int id) + public async Task> GetAsId(int id) { var result = await _service.GetAsId(id); - if (result == null) - return NotFound($"Wrong Id {id}"); - return Ok(result); + if (result == null) return NotFound($"Wrong Id {id}"); + return Ok(new StudentResponse(result)); } [HttpPost] - public async Task> AddStudent(AddStudentRequest addStudentRequest) + public async Task> AddStudent(AddStudentRequest addStudentRequest) { var request = new AddStudentRequest(); var student = request.ToStudent(addStudentRequest); var result = await _service.AddStudent(student); - return Ok(new AddStudentRequest(result)); + return Ok(new StudentResponse(result)); } [HttpPut] [Route("{id}")] - public async Task> UpdateStudent(int id, UpdateStudentRequest updateStudentRequest) + public async Task> UpdateStudent(int id, UpdateStudentRequest updateStudentRequest) { var request = new UpdateStudentRequest(); - var student = request.UpdateStudent(updateStudentRequest); + var student = request.ToUpdateStudent(updateStudentRequest); var result = await _service.UpdateStudent(id, student); if (result == null) return NotFound($"Wrong Id {id}"); - return Ok(new UpdateStudentRequest(result)); + return Ok(new StudentResponse(result)); } [HttpDelete] diff --git a/StudentApp/Data/StudentAppContext.cs b/StudentApp/Data/StudentAppContext.cs index f701251..dd6c592 100644 --- a/StudentApp/Data/StudentAppContext.cs +++ b/StudentApp/Data/StudentAppContext.cs @@ -13,6 +13,8 @@ public StudentAppContext(DbContextOptions options) : base(options) { } + public DbSet Student { get; set; } + public DbSet StudentAddress { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=StudentApp.Data;Trusted_Connection=True"); @@ -21,12 +23,10 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() - .HasOne(s => s.Address) - .WithOne(ad => ad.Students) - .HasForeignKey(ad => ad.StudentsId); + .Property(s => s.Id) + .HasColumnName("id") + .HasDefaultValue(0) + .IsRequired(); } - - public DbSet Student { get; set; } - public DbSet StudentAddress { get; set; } } } diff --git a/StudentApp/Migrations/20230113204620_InitialDb.cs b/StudentApp/Migrations/20230113204620_InitialDb.cs index 22e05cf..34806dd 100644 --- a/StudentApp/Migrations/20230113204620_InitialDb.cs +++ b/StudentApp/Migrations/20230113204620_InitialDb.cs @@ -1,4 +1,4 @@ -using System; + using Microsoft.EntityFrameworkCore.Migrations; #nullable disable diff --git a/StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs b/StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs new file mode 100644 index 0000000..9f9c9f9 --- /dev/null +++ b/StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs @@ -0,0 +1,121 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using StudentApp.Data; + +#nullable disable + +namespace StudentApp.Migrations +{ + [DbContext(typeof(StudentAppContext))] + [Migration("20230116125527_StudentAppMigration")] + partial class StudentAppMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("StudentApp.Models.StudentAddress", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("City") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Country") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PostNumber") + .HasColumnType("int"); + + b.Property("StudentsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("StudentsId") + .IsUnique(); + + b.ToTable("StudentAddress"); + }); + + modelBuilder.Entity("StudentApp.Models.Students", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RegistrationDate") + .HasColumnType("datetime2"); + + b.Property("School") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SecondName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("TlfNo") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Student"); + }); + + modelBuilder.Entity("StudentApp.Models.StudentAddress", b => + { + b.HasOne("StudentApp.Models.Students", "Students") + .WithOne("Address") + .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Students"); + }); + + modelBuilder.Entity("StudentApp.Models.Students", b => + { + b.Navigation("Address") + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/StudentApp/Migrations/20230116125527_StudentAppMigration.cs b/StudentApp/Migrations/20230116125527_StudentAppMigration.cs new file mode 100644 index 0000000..4742c2e --- /dev/null +++ b/StudentApp/Migrations/20230116125527_StudentAppMigration.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace StudentApp.Migrations +{ + /// + public partial class StudentAppMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs index 222fe81..f99e9c7 100644 --- a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs +++ b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs @@ -60,9 +60,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + .HasColumnType("int") + .HasDefaultValue(0) + .HasColumnName("id"); b.Property("FirstName") .IsRequired() @@ -99,7 +99,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentAddress", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("Address") + .WithOne("AddressStudent") .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -109,7 +109,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.Students", b => { - b.Navigation("Address") + b.Navigation("AddressStudent") .IsRequired(); }); #pragma warning restore 612, 618 diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index 1ebefee..f194173 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -17,10 +17,7 @@ public AddStudentRequest(Students students) TlfNo= students.TlfNo; School= students.School; RegistrationDate = students.RegistrationDate; - Address = students.Address.Address; - City = students.Address.City; - PostNumber = students.Address.PostNumber; - Country= students.Address.Country; + addressStudent = new AddressStudentRequest(students.AddressStudent); } [IsNotNullOrEmpty] public string UserName { get; set; } @@ -30,14 +27,11 @@ public AddStudentRequest(Students students) [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public string Address{ get; set; } - [IsNotNullOrEmpty] public string City { get; set; } - [IsNotNullOrEmpty] public int PostNumber { get; set; } - [IsNotNullOrEmpty] public string Country { get; set; } + [IsNotNullOrEmpty] public AddressStudentRequest addressStudent { get; set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { - return new Students + return new Students { UserName = addStudentRequest.UserName, FirstName = addStudentRequest.FirstName, @@ -45,14 +39,8 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) LastName = addStudentRequest.LastName, TlfNo = addStudentRequest.TlfNo, School = addStudentRequest.School, - RegistrationDate= addStudentRequest.RegistrationDate, - Address = new StudentAddress - { - Address= addStudentRequest.Address, - City = addStudentRequest.City, - PostNumber = addStudentRequest.PostNumber, - Country = addStudentRequest.Country - } + RegistrationDate = addStudentRequest.RegistrationDate, + AddressStudent = new AddressStudentRequest().ToAddressStudent(addStudentRequest.addressStudent) }; } } diff --git a/StudentApp/Models/AddressStudentRequest.cs b/StudentApp/Models/AddressStudentRequest.cs index 5b1586e..28f5d0e 100644 --- a/StudentApp/Models/AddressStudentRequest.cs +++ b/StudentApp/Models/AddressStudentRequest.cs @@ -8,12 +8,12 @@ public class AddressStudentRequest public AddressStudentRequest() { } - public AddressStudentRequest(Students students) + public AddressStudentRequest(StudentAddress studentAddress) { - Address = students.Address.Address; - City = students.Address.City; - PostNumber = students.Address.PostNumber; - Country= students.Address.Country; + Address = studentAddress.Address; + City = studentAddress.City; + PostNumber = studentAddress.PostNumber; + Country= studentAddress.Country; } [IsNotNullOrEmpty] public string Address{ get; set; } @@ -21,17 +21,14 @@ public AddressStudentRequest(Students students) [IsNotNullOrEmpty] public int PostNumber { get; set; } [IsNotNullOrEmpty] public string Country { get; set; } - public virtual Students AddressStudent(AddressStudentRequest addressStudentRequest) + public virtual StudentAddress ToAddressStudent(AddressStudentRequest addressStudentRequest) { - return new Students + return new StudentAddress { - Address = new StudentAddress - { Address= addressStudentRequest.Address, City = addressStudentRequest.City, PostNumber = addressStudentRequest.PostNumber, Country = addressStudentRequest.Country - } }; } } diff --git a/StudentApp/Models/StudentResponse.cs b/StudentApp/Models/StudentResponse.cs new file mode 100644 index 0000000..ff34f4c --- /dev/null +++ b/StudentApp/Models/StudentResponse.cs @@ -0,0 +1,12 @@ + +namespace StudentApp.Models; + +public class StudentResponse : AddStudentRequest +{ + public StudentResponse(Students students) : base(students) + { + StudentId = students.Id; + } + + public int StudentId { get; set;} +} diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index 0f19139..f233f07 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -1,13 +1,10 @@  -using System.ComponentModel.DataAnnotations; +using Nest; namespace StudentApp.Models { public class Students { - internal int id; - private readonly Students Student; - public int Id { get; set; } public string UserName { get; set; } public string FirstName { get; set; } @@ -17,7 +14,7 @@ public class Students public string School { get; set; } public DateTime RegistrationDate { get; internal set; } - public StudentAddress Address { get; set; } + public StudentAddress AddressStudent { get; set; } } public class StudentAddress @@ -29,6 +26,5 @@ public class StudentAddress public string Country { get; set; } public int StudentsId { get; set; } public Students Students { get; set; } - } } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index 3881d38..d94f258 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -16,10 +16,7 @@ public UpdateStudentRequest(Students students) TlfNo = students.TlfNo; School = students.School; RegistrationDate = students.RegistrationDate; - Address = students.Address.Address; - City = students.Address.City; - PostNumber = students.Address.PostNumber; - Country = students.Address.Country; + addressStudent = new AddressStudentRequest(students.AddressStudent); } [IsNotNullOrEmpty] public string UserName { get; set; } @@ -28,14 +25,11 @@ public UpdateStudentRequest(Students students) [IsNotNullOrEmpty] public string LastName { get; set; } [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } - [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public string Address { get; set; } - [IsNotNullOrEmpty] public string City { get; set; } - [IsNotNullOrEmpty] public int PostNumber { get; set; } - [IsNotNullOrEmpty] public string Country { get; set; } + [ValidateUpdateJoinDate] public DateTime RegistrationDate { get; set; } + [IsNotNullOrEmpty] public AddressStudentRequest addressStudent { get; set; } - public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) + public virtual Students ToUpdateStudent(UpdateStudentRequest updateStudentRequest) { return new Students { @@ -46,13 +40,7 @@ public virtual Students UpdateStudent(UpdateStudentRequest updateStudentRequest) TlfNo = updateStudentRequest.TlfNo, School = updateStudentRequest.School, RegistrationDate = updateStudentRequest.RegistrationDate, - Address = new StudentAddress - { - Address = updateStudentRequest.Address, - City = updateStudentRequest.City, - PostNumber = updateStudentRequest.PostNumber, - Country = updateStudentRequest.Country - } + AddressStudent = new AddressStudentRequest().ToAddressStudent(updateStudentRequest.addressStudent) }; } } diff --git a/StudentApp/Program.cs b/StudentApp/Program.cs index 51b0099..72ea632 100644 --- a/StudentApp/Program.cs +++ b/StudentApp/Program.cs @@ -1,4 +1,4 @@ - +#region using using Microsoft.EntityFrameworkCore; using StudentApp.Data; using StudentApp.Repo; @@ -7,6 +7,7 @@ using Microsoft.Identity.Web; using Microsoft.OpenApi.Models; using System.Text.Json.Serialization; +#endregion var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); diff --git a/StudentApp/Repo/Repo.cs b/StudentApp/Repo/Repo.cs index 850b66b..898fc68 100644 --- a/StudentApp/Repo/Repo.cs +++ b/StudentApp/Repo/Repo.cs @@ -1,9 +1,7 @@  -using Microsoft.Azure.Amqp.Framing; using Microsoft.EntityFrameworkCore; using StudentApp.Data; using StudentApp.Models; -using System.Diagnostics.Metrics; namespace StudentApp.Repo; @@ -16,11 +14,11 @@ public Repo(StudentAppContext context) _context = context; } - public Task> Get() => - _context.Student.ToListAsync(); + public async Task> Get() => + await _context.Student.Include(s => s.AddressStudent).ToListAsync(); - public async Task GetAsId(int id) => - await _context.Student.FirstOrDefaultAsync(s => s.Id == id); + public async Task GetAsId(int id) => + await _context.Student.Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); public async Task AddStudent(Students students) { @@ -41,7 +39,7 @@ public async Task UpdateStudent(int id, Students students) result.TlfNo = students.TlfNo; result.School = students.School; result.RegistrationDate= students.RegistrationDate; - result.Address = students.Address; + result.AddressStudent = students.AddressStudent; _context.Student.Update(result); await _context.SaveChangesAsync(); @@ -63,17 +61,9 @@ public async Task UpdateStudent(int id, Students students) await _context.SaveChangesAsync(); return true; } - catch (Exception ex) + catch (Exception) { return false; } } -} - - - - - - - - +} \ No newline at end of file diff --git a/StudentApp/StudentApp.csproj b/StudentApp/StudentApp.csproj index be96ced..ce76fc0 100644 --- a/StudentApp/StudentApp.csproj +++ b/StudentApp/StudentApp.csproj @@ -9,7 +9,6 @@ - @@ -31,7 +30,6 @@ - @@ -39,4 +37,8 @@ + + + + diff --git a/StudentApp/Views/Students/Create.cshtml b/StudentApp/Views/Students/Create.cshtml index 61322f2..d6f007d 100644 --- a/StudentApp/Views/Students/Create.cshtml +++ b/StudentApp/Views/Students/Create.cshtml @@ -28,9 +28,9 @@
- - - + + +
diff --git a/StudentApp/Views/Students/Delete.cshtml b/StudentApp/Views/Students/Delete.cshtml index ccf30d8..8c3c6e3 100644 --- a/StudentApp/Views/Students/Delete.cshtml +++ b/StudentApp/Views/Students/Delete.cshtml @@ -30,10 +30,10 @@ @Html.DisplayFor(model => model.LastName)
- @Html.DisplayNameFor(model => model.Address) + @Html.DisplayNameFor(model => model.AddressStudent)
- @Html.DisplayFor(model => model.Address) + @Html.DisplayFor(model => model.AddressStudent)
@Html.DisplayNameFor(model => model.TlfNo) diff --git a/StudentApp/Views/Students/Details.cshtml b/StudentApp/Views/Students/Details.cshtml index 2d4ce24..03b4201 100644 --- a/StudentApp/Views/Students/Details.cshtml +++ b/StudentApp/Views/Students/Details.cshtml @@ -29,10 +29,10 @@ @Html.DisplayFor(model => model.LastName)
- @Html.DisplayNameFor(model => model.Address) + @Html.DisplayNameFor(model => model.AddressStudent)
- @Html.DisplayFor(model => model.Address) + @Html.DisplayFor(model => model.AddressStudent)
@Html.DisplayNameFor(model => model.TlfNo) diff --git a/StudentApp/Views/Students/Edit.cshtml b/StudentApp/Views/Students/Edit.cshtml index e5967ba..1ff5096 100644 --- a/StudentApp/Views/Students/Edit.cshtml +++ b/StudentApp/Views/Students/Edit.cshtml @@ -29,9 +29,9 @@
- - - + + +
diff --git a/StudentApp/Views/Students/Index.cshtml b/StudentApp/Views/Students/Index.cshtml index 14c3eb1..6ff5cf0 100644 --- a/StudentApp/Views/Students/Index.cshtml +++ b/StudentApp/Views/Students/Index.cshtml @@ -22,7 +22,7 @@ @Html.DisplayNameFor(model => model.LastName) - @Html.DisplayNameFor(model => model.Address) + @Html.DisplayNameFor(model => model.AddressStudent) @Html.DisplayNameFor(model => model.TlfNo) @@ -46,7 +46,7 @@ @Html.DisplayFor(modelItem => item.LastName) - @Html.DisplayFor(modelItem => item.Address) + @Html.DisplayFor(modelItem => item.AddressStudent) @Html.DisplayFor(modelItem => item.TlfNo) From 4f4098d1788b6510d2a73945a291e8c7d6c3711c Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Wed, 25 Jan 2023 22:20:21 +0100 Subject: [PATCH 13/69] Include UpdateStudentRequest --- StudentApp/Repo/Repo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/StudentApp/Repo/Repo.cs b/StudentApp/Repo/Repo.cs index 898fc68..39e6252 100644 --- a/StudentApp/Repo/Repo.cs +++ b/StudentApp/Repo/Repo.cs @@ -29,7 +29,7 @@ public async Task AddStudent(Students students) public async Task UpdateStudent(int id, Students students) { - var result = await _context.Student.FirstOrDefaultAsync(s => s.Id == id); + var result = await _context.Student.Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); if (result != null) { result.UserName = students.UserName; From cdcfdba4b1f73ebd354683bf873f218dab7cf110 Mon Sep 17 00:00:00 2001 From: Ahmet Ulucay <57094137+ahmetulucay@users.noreply.github.com> Date: Wed, 25 Jan 2023 22:27:44 +0100 Subject: [PATCH 14/69] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ad15cae..192c841 100644 --- a/README.md +++ b/README.md @@ -184,8 +184,8 @@ Making changes on Azure Portal = - now you can click save/configure "Configure single-page application". Dont close RUNNING program. !!! -GO BACK TO Running program, Swagger page= ----------------------------------------- +BACK TO Running program, Swagger page= +-------------------------------------- - Click ("Authorize") button, - on the new page click data ("api://...................") under "Scopes" and then click "Authorize" button. - on the new opened page Sign in with your account (account logging information in which you created SwaggerAzureADApi) From 841c30bf7b079e57a6c4405b0bdb7c28e8c7835e Mon Sep 17 00:00:00 2001 From: Ahmet Ulucay <57094137+ahmetulucay@users.noreply.github.com> Date: Wed, 25 Jan 2023 22:29:34 +0100 Subject: [PATCH 15/69] Update README.md --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 192c841..a588383 100644 --- a/README.md +++ b/README.md @@ -139,23 +139,23 @@ Making changes on project/StudentApp = }, "SwaggerAzureAD": { "AuthorizationUrl": "https://login.microsoftonline.com/d5af55b1-09d1-4c62-b91b-d108fd981704/oauth2/v2.0/authorize", - >>>> take this data from "SwaggerClientAppRegistration | Endpoints" (authorization endpoint(v2)) + >> take this data from "SwaggerClientAppRegistration | Endpoints" (authorization endpoint(v2)) "TokenUrl": "https://login.microsoftonline.com/d5af55b1-09d1-4c62-b91b-d108fd981704/oauth2/v2.0/token", - >>>> take this data from "SwaggerClientAppRegistration | Endpoints" (token endpoint(v2)) + >> take this data from "SwaggerClientAppRegistration | Endpoints" (token endpoint(v2)) "Scope": "api://a3f042ab-660c-4b16-92ee-6c1ea013f75e/access_as_user", - >>>> go to "SwaggerClientAppRegistration | API permissions", - >>>> click (+ Add a Permission) on the new page click "My APIs", - >>>> click project api name (exp. SwaggerAzureADApi) on the new page click "access_as_user", - >>>> on the same page click (Add permissions) - >>>> we are again on the "SwaggerClientAppRegistration | API permissions" page, - >>>> click "access_as_user" and on the new page copy data "api://......................." - >>>> paste this data to "Scope". + >> go to "SwaggerClientAppRegistration | API permissions", + >> click (+ Add a Permission) on the new page click "My APIs", + >> click project api name (exp. SwaggerAzureADApi) on the new page click "access_as_user", + >> on the same page click (Add permissions) + >> we are again on the "SwaggerClientAppRegistration | API permissions" page, + >> click "access_as_user" and on the new page copy data "api://......................." + >> paste this data to "Scope". "ClientId": "e6e468b6-e2bd-405f-8808-40aef44b49da" - >>>> take this data from "SwaggerClientAppRegistration | Overview" section (Application (client) ID) + >> take this data from "SwaggerClientAppRegistration | Overview" section (Application (client) ID) } @@ -178,7 +178,7 @@ Making changes on Azure Portal = - click (+ Add a platform) - on the new page click ("Single-page application"), - on the new opened page click ("Access tokens" and "ID tokens") but don't save because we need "Redirect URIs", -- so NOW, RUN program from Visual Studio/-code, !!! +- NOW, RUN program from Visual Studio/-code, !!! and on the opened page copy the url (exp."https://localhost:7019/swagger") - and paste it to "Redirect URIs" as (exp."https://localhost:7019/swagger/oauth2-redirect.html"), - now you can click save/configure "Configure single-page application". Dont close RUNNING program. !!! From ae9b9e01afee78e3cf38c411a4c946ac210f3cbb Mon Sep 17 00:00:00 2001 From: Ahmet Ulucay <57094137+ahmetulucay@users.noreply.github.com> Date: Wed, 25 Jan 2023 22:31:42 +0100 Subject: [PATCH 16/69] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a588383..ef71c7d 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Making changes on project using data from Azure Portal = ![SwaggerAzureADApi App regs](https://user-images.githubusercontent.com/57094137/210285097-b0dc9055-def3-4d04-a382-feeca68fd0c0.jpg) -RUN project whether it is working or not = +RUN project to check it is working or not = ------------------------------------------ - run get method and execute, - if you get response as "Status Code 401", it is okay and we need to make some changes in Program.cs. From 22a0dcf0e8b86b2471252fe3cb370564128f663e Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Thu, 26 Jan 2023 00:44:26 +0100 Subject: [PATCH 17/69] Adding PhoneNo and EmailAddress --- ...3_UpdateStudentsAddressTables.Designer.cs} | 88 +++++++++++++++++-- ...0125233033_UpdateStudentsAddressTables.cs} | 70 +++++++++++++-- .../StudentAppContextModelSnapshot.cs | 78 +++++++++++++++- StudentApp/Models/AddStudentRequest.cs | 15 ++-- .../Models/EmailAddressStudentRequest.cs | 25 ++++++ StudentApp/Models/ErrorViewModel.cs | 2 +- StudentApp/Models/PhoneStudentRequest.cs | 25 ++++++ StudentApp/Models/Students.cs | 21 ++++- StudentApp/Models/UpdateStudentRequest.cs | 15 ++-- StudentApp/Repo/Repo.cs | 9 +- StudentApp/Views/Students/Create.cshtml | 5 -- StudentApp/Views/Students/Delete.cshtml | 6 -- StudentApp/Views/Students/Details.cshtml | 6 -- StudentApp/Views/Students/Edit.cshtml | 5 -- StudentApp/Views/Students/Index.cshtml | 6 -- 15 files changed, 308 insertions(+), 68 deletions(-) rename StudentApp/Migrations/{20230113204620_InitialDb.Designer.cs => 20230125233033_UpdateStudentsAddressTables.Designer.cs} (58%) rename StudentApp/Migrations/{20230113204620_InitialDb.cs => 20230125233033_UpdateStudentsAddressTables.cs} (51%) create mode 100644 StudentApp/Models/EmailAddressStudentRequest.cs create mode 100644 StudentApp/Models/PhoneStudentRequest.cs diff --git a/StudentApp/Migrations/20230113204620_InitialDb.Designer.cs b/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.Designer.cs similarity index 58% rename from StudentApp/Migrations/20230113204620_InitialDb.Designer.cs rename to StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.Designer.cs index 5c72426..bd49cd2 100644 --- a/StudentApp/Migrations/20230113204620_InitialDb.Designer.cs +++ b/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.Designer.cs @@ -12,8 +12,8 @@ namespace StudentApp.Migrations { [DbContext(typeof(StudentAppContext))] - [Migration("20230113204620_InitialDb")] - partial class InitialDb + [Migration("20230125233033_UpdateStudentsAddressTables")] + partial class UpdateStudentsAddressTables { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -59,7 +59,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.ToTable("StudentAddress"); }); - modelBuilder.Entity("StudentApp.Models.Students", b => + modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -67,6 +67,52 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("EmailAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StudentsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("StudentsId") + .IsUnique(); + + b.ToTable("StudentEmailAddress"); + }); + + modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PhoneNo") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StudentsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("StudentsId") + .IsUnique(); + + b.ToTable("StudentPhoneNo"); + }); + + modelBuilder.Entity("StudentApp.Models.Students", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasDefaultValue(0) + .HasColumnName("id"); + b.Property("FirstName") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -86,10 +132,6 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("TlfNo") - .IsRequired() - .HasColumnType("nvarchar(max)"); - b.Property("UserName") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -102,7 +144,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentAddress", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("Address") + .WithOne("AddressStudent") .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -110,9 +152,37 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Navigation("Students"); }); + modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => + { + b.HasOne("StudentApp.Models.Students", "Students") + .WithOne("EmailAddressStudent") + .HasForeignKey("StudentApp.Models.StudentEmailAddress", "StudentsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Students"); + }); + + modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => + { + b.HasOne("StudentApp.Models.Students", "Students") + .WithOne("PhoneStudent") + .HasForeignKey("StudentApp.Models.StudentPhoneNo", "StudentsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Students"); + }); + modelBuilder.Entity("StudentApp.Models.Students", b => { - b.Navigation("Address") + b.Navigation("AddressStudent") + .IsRequired(); + + b.Navigation("EmailAddressStudent") + .IsRequired(); + + b.Navigation("PhoneStudent") .IsRequired(); }); #pragma warning restore 612, 618 diff --git a/StudentApp/Migrations/20230113204620_InitialDb.cs b/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.cs similarity index 51% rename from StudentApp/Migrations/20230113204620_InitialDb.cs rename to StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.cs index 34806dd..6827570 100644 --- a/StudentApp/Migrations/20230113204620_InitialDb.cs +++ b/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.cs @@ -1,4 +1,4 @@ - +using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable @@ -6,7 +6,7 @@ namespace StudentApp.Migrations { /// - public partial class InitialDb : Migration + public partial class UpdateStudentsAddressTables : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -15,19 +15,17 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "Student", columns: table => new { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), + id = table.Column(type: "int", nullable: false, defaultValue: 0), UserName = table.Column(type: "nvarchar(max)", nullable: false), FirstName = table.Column(type: "nvarchar(max)", nullable: false), SecondName = table.Column(type: "nvarchar(max)", nullable: false), LastName = table.Column(type: "nvarchar(max)", nullable: false), - TlfNo = table.Column(type: "nvarchar(max)", nullable: false), School = table.Column(type: "nvarchar(max)", nullable: false), RegistrationDate = table.Column(type: "datetime2", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Student", x => x.Id); + table.PrimaryKey("PK_Student", x => x.id); }); migrationBuilder.CreateTable( @@ -49,7 +47,47 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "FK_StudentAddress_Student_StudentsId", column: x => x.StudentsId, principalTable: "Student", - principalColumn: "Id", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "StudentEmailAddress", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + EmailAddress = table.Column(type: "nvarchar(max)", nullable: false), + StudentsId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_StudentEmailAddress", x => x.Id); + table.ForeignKey( + name: "FK_StudentEmailAddress_Student_StudentsId", + column: x => x.StudentsId, + principalTable: "Student", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "StudentPhoneNo", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PhoneNo = table.Column(type: "nvarchar(max)", nullable: false), + StudentsId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_StudentPhoneNo", x => x.Id); + table.ForeignKey( + name: "FK_StudentPhoneNo_Student_StudentsId", + column: x => x.StudentsId, + principalTable: "Student", + principalColumn: "id", onDelete: ReferentialAction.Cascade); }); @@ -58,6 +96,18 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "StudentAddress", column: "StudentsId", unique: true); + + migrationBuilder.CreateIndex( + name: "IX_StudentEmailAddress_StudentsId", + table: "StudentEmailAddress", + column: "StudentsId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_StudentPhoneNo_StudentsId", + table: "StudentPhoneNo", + column: "StudentsId", + unique: true); } /// @@ -66,6 +116,12 @@ protected override void Down(MigrationBuilder migrationBuilder) migrationBuilder.DropTable( name: "StudentAddress"); + migrationBuilder.DropTable( + name: "StudentEmailAddress"); + + migrationBuilder.DropTable( + name: "StudentPhoneNo"); + migrationBuilder.DropTable( name: "Student"); } diff --git a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs index f99e9c7..5eda90c 100644 --- a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs +++ b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs @@ -56,6 +56,52 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("StudentAddress"); }); + modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("EmailAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StudentsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("StudentsId") + .IsUnique(); + + b.ToTable("StudentEmailAddress"); + }); + + modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PhoneNo") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StudentsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("StudentsId") + .IsUnique(); + + b.ToTable("StudentPhoneNo"); + }); + modelBuilder.Entity("StudentApp.Models.Students", b => { b.Property("Id") @@ -83,10 +129,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("TlfNo") - .IsRequired() - .HasColumnType("nvarchar(max)"); - b.Property("UserName") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -107,10 +149,38 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("Students"); }); + modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => + { + b.HasOne("StudentApp.Models.Students", "Students") + .WithOne("EmailAddressStudent") + .HasForeignKey("StudentApp.Models.StudentEmailAddress", "StudentsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Students"); + }); + + modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => + { + b.HasOne("StudentApp.Models.Students", "Students") + .WithOne("PhoneStudent") + .HasForeignKey("StudentApp.Models.StudentPhoneNo", "StudentsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Students"); + }); + modelBuilder.Entity("StudentApp.Models.Students", b => { b.Navigation("AddressStudent") .IsRequired(); + + b.Navigation("EmailAddressStudent") + .IsRequired(); + + b.Navigation("PhoneStudent") + .IsRequired(); }); #pragma warning restore 612, 618 } diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index f194173..d56dad7 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -14,20 +14,22 @@ public AddStudentRequest(Students students) FirstName= students.FirstName; SecondName = students.SecondName; LastName= students.LastName; - TlfNo= students.TlfNo; School= students.School; RegistrationDate = students.RegistrationDate; - addressStudent = new AddressStudentRequest(students.AddressStudent); + PhoneStudent = new PhoneStudentRequest(students.PhoneStudent); + EmailAddressStudent = new EmailAddressStudentRequest(students.EmailAddressStudent); + AddressStudent = new AddressStudentRequest(students.AddressStudent); } [IsNotNullOrEmpty] public string UserName { get; set; } [IsNotNullOrEmpty] public string FirstName { get; set; } public string SecondName { get; set; } [IsNotNullOrEmpty] public string LastName { get; set; } - [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public AddressStudentRequest addressStudent { get; set; } + [IsNotNullOrEmpty] public PhoneStudentRequest PhoneStudent { get; set; } + [IsNotNullOrEmpty] public EmailAddressStudentRequest EmailAddressStudent { get; set; } + [IsNotNullOrEmpty] public AddressStudentRequest AddressStudent { get; set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { @@ -37,10 +39,11 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) FirstName = addStudentRequest.FirstName, SecondName = addStudentRequest.SecondName, LastName = addStudentRequest.LastName, - TlfNo = addStudentRequest.TlfNo, School = addStudentRequest.School, RegistrationDate = addStudentRequest.RegistrationDate, - AddressStudent = new AddressStudentRequest().ToAddressStudent(addStudentRequest.addressStudent) + PhoneStudent = new PhoneStudentRequest().ToPhoneStudent(addStudentRequest.PhoneStudent), + EmailAddressStudent = new EmailAddressStudentRequest().ToEmailStudent(addStudentRequest.EmailAddressStudent), + AddressStudent = new AddressStudentRequest().ToAddressStudent(addStudentRequest.AddressStudent) }; } } diff --git a/StudentApp/Models/EmailAddressStudentRequest.cs b/StudentApp/Models/EmailAddressStudentRequest.cs new file mode 100644 index 0000000..3240ce0 --- /dev/null +++ b/StudentApp/Models/EmailAddressStudentRequest.cs @@ -0,0 +1,25 @@ + +using StudentApp.Controllers.Validations; + +namespace StudentApp.Models +{ + public class EmailAddressStudentRequest + { + public EmailAddressStudentRequest() + { + } + public EmailAddressStudentRequest(StudentEmailAddress studentEmailAddress) + { + EmailAddress = studentEmailAddress.EmailAddress; + } + [IsNotNullOrEmpty] public string EmailAddress { get; private set; } + + public virtual StudentEmailAddress ToEmailStudent(EmailAddressStudentRequest emailAddressStudentRequest) + { + return new StudentEmailAddress + { + EmailAddress = emailAddressStudentRequest.EmailAddress + }; + } + } +} diff --git a/StudentApp/Models/ErrorViewModel.cs b/StudentApp/Models/ErrorViewModel.cs index eedb0c2..b30b4cb 100644 --- a/StudentApp/Models/ErrorViewModel.cs +++ b/StudentApp/Models/ErrorViewModel.cs @@ -7,4 +7,4 @@ public class ErrorViewModel public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); } -} \ No newline at end of file +} diff --git a/StudentApp/Models/PhoneStudentRequest.cs b/StudentApp/Models/PhoneStudentRequest.cs new file mode 100644 index 0000000..f81b643 --- /dev/null +++ b/StudentApp/Models/PhoneStudentRequest.cs @@ -0,0 +1,25 @@ + +using StudentApp.Controllers.Validations; + +namespace StudentApp.Models +{ + public class PhoneStudentRequest + { + public PhoneStudentRequest() + { + } + public PhoneStudentRequest(StudentPhoneNo studentPhone) + { + PhoneNo = studentPhone.PhoneNo; + } + [IsNotNullOrEmpty] public string PhoneNo { get; private set; } + + public virtual StudentPhoneNo ToPhoneStudent(PhoneStudentRequest phoneStudentRequest) + { + return new StudentPhoneNo + { + PhoneNo = phoneStudentRequest.PhoneNo, + }; + } + } +} diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index f233f07..d14e1e5 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -1,6 +1,4 @@  -using Nest; - namespace StudentApp.Models { public class Students @@ -10,13 +8,30 @@ public class Students public string FirstName { get; set; } public string SecondName { get; set; } public string LastName { get; set; } - public string TlfNo { get; set; } public string School { get; set; } public DateTime RegistrationDate { get; internal set; } + public StudentPhoneNo PhoneStudent { get; set; } + public StudentEmailAddress EmailAddressStudent { get; set; } public StudentAddress AddressStudent { get; set; } } + public class StudentPhoneNo + { + public int Id { get; set; } + public string PhoneNo { get; set; } + public int StudentsId { get; set; } + public Students Students { get; set; } + } + + public class StudentEmailAddress + { + public int Id { get; set; } + public string EmailAddress { get; set; } + public int StudentsId { get; set; } + public Students Students { get; set; } + + } public class StudentAddress { public int Id { get; set; } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index d94f258..e96a5d0 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -13,20 +13,22 @@ public UpdateStudentRequest(Students students) FirstName = students.FirstName; SecondName = students.SecondName; LastName = students.LastName; - TlfNo = students.TlfNo; School = students.School; RegistrationDate = students.RegistrationDate; - addressStudent = new AddressStudentRequest(students.AddressStudent); + PhoneStudent = new PhoneStudentRequest(students.PhoneStudent); + EmailAddressStudent = new EmailAddressStudentRequest(students.EmailAddressStudent); + AddressStudent = new AddressStudentRequest(students.AddressStudent); } [IsNotNullOrEmpty] public string UserName { get; set; } [IsNotNullOrEmpty] public string FirstName { get; set; } public string SecondName { get; set; } [IsNotNullOrEmpty] public string LastName { get; set; } - [IsNotNullOrEmpty] public string TlfNo { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateUpdateJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public AddressStudentRequest addressStudent { get; set; } + [IsNotNullOrEmpty] public PhoneStudentRequest PhoneStudent { get; set; } + [IsNotNullOrEmpty] public EmailAddressStudentRequest EmailAddressStudent { get; set; } + [IsNotNullOrEmpty] public AddressStudentRequest AddressStudent { get; set; } public virtual Students ToUpdateStudent(UpdateStudentRequest updateStudentRequest) @@ -37,10 +39,11 @@ public virtual Students ToUpdateStudent(UpdateStudentRequest updateStudentReques FirstName = updateStudentRequest.FirstName, SecondName = updateStudentRequest.SecondName, LastName = updateStudentRequest.LastName, - TlfNo = updateStudentRequest.TlfNo, School = updateStudentRequest.School, RegistrationDate = updateStudentRequest.RegistrationDate, - AddressStudent = new AddressStudentRequest().ToAddressStudent(updateStudentRequest.addressStudent) + PhoneStudent = new PhoneStudentRequest().ToPhoneStudent(updateStudentRequest.PhoneStudent), + EmailAddressStudent = new EmailAddressStudentRequest().ToEmailStudent(updateStudentRequest.EmailAddressStudent), + AddressStudent = new AddressStudentRequest().ToAddressStudent(updateStudentRequest.AddressStudent) }; } } diff --git a/StudentApp/Repo/Repo.cs b/StudentApp/Repo/Repo.cs index 39e6252..86a5cce 100644 --- a/StudentApp/Repo/Repo.cs +++ b/StudentApp/Repo/Repo.cs @@ -15,10 +15,10 @@ public Repo(StudentAppContext context) } public async Task> Get() => - await _context.Student.Include(s => s.AddressStudent).ToListAsync(); + await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).ToListAsync(); public async Task GetAsId(int id) => - await _context.Student.Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); + await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); public async Task AddStudent(Students students) { @@ -29,16 +29,17 @@ public async Task AddStudent(Students students) public async Task UpdateStudent(int id, Students students) { - var result = await _context.Student.Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); + var result = await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); if (result != null) { result.UserName = students.UserName; result.FirstName = students.FirstName; result.SecondName = students.SecondName; result.LastName = students.LastName; - result.TlfNo = students.TlfNo; result.School = students.School; result.RegistrationDate= students.RegistrationDate; + result.PhoneStudent = students.PhoneStudent; + result.EmailAddressStudent= students.EmailAddressStudent; result.AddressStudent = students.AddressStudent; _context.Student.Update(result); diff --git a/StudentApp/Views/Students/Create.cshtml b/StudentApp/Views/Students/Create.cshtml index d6f007d..4231761 100644 --- a/StudentApp/Views/Students/Create.cshtml +++ b/StudentApp/Views/Students/Create.cshtml @@ -32,11 +32,6 @@
-
- - - -
diff --git a/StudentApp/Views/Students/Delete.cshtml b/StudentApp/Views/Students/Delete.cshtml index 8c3c6e3..83dfdb1 100644 --- a/StudentApp/Views/Students/Delete.cshtml +++ b/StudentApp/Views/Students/Delete.cshtml @@ -35,12 +35,6 @@
@Html.DisplayFor(model => model.AddressStudent)
-
- @Html.DisplayNameFor(model => model.TlfNo) -
-
- @Html.DisplayFor(model => model.TlfNo) -
@Html.DisplayNameFor(model => model.School)
diff --git a/StudentApp/Views/Students/Details.cshtml b/StudentApp/Views/Students/Details.cshtml index 03b4201..e497327 100644 --- a/StudentApp/Views/Students/Details.cshtml +++ b/StudentApp/Views/Students/Details.cshtml @@ -34,12 +34,6 @@
@Html.DisplayFor(model => model.AddressStudent)
-
- @Html.DisplayNameFor(model => model.TlfNo) -
-
- @Html.DisplayFor(model => model.TlfNo) -
@Html.DisplayNameFor(model => model.School)
diff --git a/StudentApp/Views/Students/Edit.cshtml b/StudentApp/Views/Students/Edit.cshtml index 1ff5096..972265d 100644 --- a/StudentApp/Views/Students/Edit.cshtml +++ b/StudentApp/Views/Students/Edit.cshtml @@ -33,11 +33,6 @@
-
- - - -
diff --git a/StudentApp/Views/Students/Index.cshtml b/StudentApp/Views/Students/Index.cshtml index 6ff5cf0..aaa1208 100644 --- a/StudentApp/Views/Students/Index.cshtml +++ b/StudentApp/Views/Students/Index.cshtml @@ -24,9 +24,6 @@ @Html.DisplayNameFor(model => model.AddressStudent) - - @Html.DisplayNameFor(model => model.TlfNo) - @Html.DisplayNameFor(model => model.School) @@ -48,9 +45,6 @@ @Html.DisplayFor(modelItem => item.AddressStudent) - - @Html.DisplayFor(modelItem => item.TlfNo) - @Html.DisplayFor(modelItem => item.School) From aa5307f65a44508fc1e553485ae3c56b0c653ce1 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Sun, 29 Jan 2023 00:12:30 +0100 Subject: [PATCH 18/69] ICollection --- StudentApp/Controllers/StudentsController.cs | 10 +- StudentApp/Data/StudentAppContext.cs | 33 +++-- ...0116125527_StudentAppMigration.Designer.cs | 121 ------------------ .../20230116125527_StudentAppMigration.cs | 22 ---- ...127094453_UpdateStudentTables.Designer.cs} | 62 ++++----- ... => 20230127094453_UpdateStudentTables.cs} | 34 +++-- .../StudentAppContextModelSnapshot.cs | 58 ++++----- StudentApp/Models/AddStudentRequest.cs | 6 +- StudentApp/Models/AddressStudentRequest.cs | 4 + .../Models/EmailAddressStudentRequest.cs | 7 +- StudentApp/Models/PhoneStudentRequest.cs | 6 +- StudentApp/Models/StudentResponse.cs | 2 +- StudentApp/Models/Students.cs | 26 ++-- StudentApp/Models/UpdateStudentRequest.cs | 6 +- StudentApp/Repo/Repo.cs | 5 +- StudentApp/Views/Students/Delete.cshtml | 2 +- StudentApp/Views/Students/Details.cshtml | 2 +- StudentApp/Views/Students/Edit.cshtml | 2 +- StudentApp/Views/Students/Index.cshtml | 6 +- 19 files changed, 148 insertions(+), 266 deletions(-) delete mode 100644 StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs delete mode 100644 StudentApp/Migrations/20230116125527_StudentAppMigration.cs rename StudentApp/Migrations/{20230125233033_UpdateStudentsAddressTables.Designer.cs => 20230127094453_UpdateStudentTables.Designer.cs} (76%) rename StudentApp/Migrations/{20230125233033_UpdateStudentsAddressTables.cs => 20230127094453_UpdateStudentTables.cs} (84%) diff --git a/StudentApp/Controllers/StudentsController.cs b/StudentApp/Controllers/StudentsController.cs index d55220f..e8d3bc3 100644 --- a/StudentApp/Controllers/StudentsController.cs +++ b/StudentApp/Controllers/StudentsController.cs @@ -42,25 +42,25 @@ public async Task> GetAsId(int id) } [HttpPost] - public async Task> AddStudent(AddStudentRequest addStudentRequest) + public async Task> AddStudent(AddStudentRequest addStudentRequest) { var request = new AddStudentRequest(); var student = request.ToStudent(addStudentRequest); var result = await _service.AddStudent(student); - return Ok(new StudentResponse(result)); + return (ICollection)Ok(new StudentResponse(result)); } [HttpPut] [Route("{id}")] - public async Task> UpdateStudent(int id, UpdateStudentRequest updateStudentRequest) + public async Task> UpdateStudent(int id, UpdateStudentRequest updateStudentRequest) { var request = new UpdateStudentRequest(); var student = request.ToUpdateStudent(updateStudentRequest); var result = await _service.UpdateStudent(id, student); if (result == null) - return NotFound($"Wrong Id {id}"); - return Ok(new StudentResponse(result)); + return (ICollection)NotFound($"Wrong Id {id}"); + return (ICollection)Ok(new StudentResponse(result)); } [HttpDelete] diff --git a/StudentApp/Data/StudentAppContext.cs b/StudentApp/Data/StudentAppContext.cs index dd6c592..8e1361a 100644 --- a/StudentApp/Data/StudentAppContext.cs +++ b/StudentApp/Data/StudentAppContext.cs @@ -13,20 +13,35 @@ public StudentAppContext(DbContextOptions options) : base(options) { } - public DbSet Student { get; set; } - public DbSet StudentAddress { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=StudentApp.Data;Trusted_Connection=True"); } - + public DbSet Student { get; set; } + public DbSet StudentPhoneNo { get; set; } + public DbSet StudentEmailAddress { get; set; } + public DbSet StudentAddress { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity() - .Property(s => s.Id) - .HasColumnName("id") - .HasDefaultValue(0) - .IsRequired(); + { + modelBuilder.Entity() + .HasOne(e => e.Students) + .WithMany(d => d.PhoneStudent) + .HasForeignKey(e => e.StudentsId) + .IsRequired(false); + + modelBuilder.Entity() + .HasOne(e => e.Students) + .WithMany(d => d.EmailAddressStudent) + .HasForeignKey(e => e.StudentsId) + .IsRequired(false); + + modelBuilder.Entity() + .HasOne(e => e.Students) + .WithMany(d => d.AddressStudent) + .HasForeignKey(e => e.StudentsId) + .IsRequired(false); + } } } -} +} \ No newline at end of file diff --git a/StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs b/StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs deleted file mode 100644 index 9f9c9f9..0000000 --- a/StudentApp/Migrations/20230116125527_StudentAppMigration.Designer.cs +++ /dev/null @@ -1,121 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using StudentApp.Data; - -#nullable disable - -namespace StudentApp.Migrations -{ - [DbContext(typeof(StudentAppContext))] - [Migration("20230116125527_StudentAppMigration")] - partial class StudentAppMigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.0") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("StudentApp.Models.StudentAddress", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Address") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("City") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Country") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("PostNumber") - .HasColumnType("int"); - - b.Property("StudentsId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("StudentsId") - .IsUnique(); - - b.ToTable("StudentAddress"); - }); - - modelBuilder.Entity("StudentApp.Models.Students", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("FirstName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("LastName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("RegistrationDate") - .HasColumnType("datetime2"); - - b.Property("School") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("SecondName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("TlfNo") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("UserName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("Student"); - }); - - modelBuilder.Entity("StudentApp.Models.StudentAddress", b => - { - b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("Address") - .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Students"); - }); - - modelBuilder.Entity("StudentApp.Models.Students", b => - { - b.Navigation("Address") - .IsRequired(); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/StudentApp/Migrations/20230116125527_StudentAppMigration.cs b/StudentApp/Migrations/20230116125527_StudentAppMigration.cs deleted file mode 100644 index 4742c2e..0000000 --- a/StudentApp/Migrations/20230116125527_StudentAppMigration.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace StudentApp.Migrations -{ - /// - public partial class StudentAppMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - - } - } -} diff --git a/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.Designer.cs b/StudentApp/Migrations/20230127094453_UpdateStudentTables.Designer.cs similarity index 76% rename from StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.Designer.cs rename to StudentApp/Migrations/20230127094453_UpdateStudentTables.Designer.cs index bd49cd2..fbb7eb8 100644 --- a/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.Designer.cs +++ b/StudentApp/Migrations/20230127094453_UpdateStudentTables.Designer.cs @@ -12,8 +12,8 @@ namespace StudentApp.Migrations { [DbContext(typeof(StudentAppContext))] - [Migration("20230125233033_UpdateStudentsAddressTables")] - partial class UpdateStudentsAddressTables + [Migration("20230127094453_UpdateStudentTables")] + partial class UpdateStudentTables { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -27,11 +27,11 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentAddress", b => { - b.Property("Id") + b.Property("AddressId") .ValueGeneratedOnAdd() .HasColumnType("int"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("AddressId")); b.Property("Address") .IsRequired() @@ -51,21 +51,20 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("StudentsId") .HasColumnType("int"); - b.HasKey("Id"); + b.HasKey("AddressId"); - b.HasIndex("StudentsId") - .IsUnique(); + b.HasIndex("StudentsId"); b.ToTable("StudentAddress"); }); modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => { - b.Property("Id") + b.Property("EmailId") .ValueGeneratedOnAdd() .HasColumnType("int"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("EmailId")); b.Property("EmailAddress") .IsRequired() @@ -74,21 +73,20 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("StudentsId") .HasColumnType("int"); - b.HasKey("Id"); + b.HasKey("EmailId"); - b.HasIndex("StudentsId") - .IsUnique(); + b.HasIndex("StudentsId"); b.ToTable("StudentEmailAddress"); }); modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => { - b.Property("Id") + b.Property("PhoneId") .ValueGeneratedOnAdd() .HasColumnType("int"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("PhoneId")); b.Property("PhoneNo") .IsRequired() @@ -97,21 +95,20 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("StudentsId") .HasColumnType("int"); - b.HasKey("Id"); + b.HasKey("PhoneId"); - b.HasIndex("StudentsId") - .IsUnique(); + b.HasIndex("StudentsId"); b.ToTable("StudentPhoneNo"); }); modelBuilder.Entity("StudentApp.Models.Students", b => { - b.Property("Id") + b.Property("StudentId") .ValueGeneratedOnAdd() - .HasColumnType("int") - .HasDefaultValue(0) - .HasColumnName("id"); + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("StudentId")); b.Property("FirstName") .IsRequired() @@ -136,7 +133,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.HasKey("Id"); + b.HasKey("StudentId"); b.ToTable("Student"); }); @@ -144,8 +141,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentAddress", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("AddressStudent") - .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") + .WithMany("AddressStudent") + .HasForeignKey("StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -155,8 +152,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("EmailAddressStudent") - .HasForeignKey("StudentApp.Models.StudentEmailAddress", "StudentsId") + .WithMany("EmailAddressStudent") + .HasForeignKey("StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -166,8 +163,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("PhoneStudent") - .HasForeignKey("StudentApp.Models.StudentPhoneNo", "StudentsId") + .WithMany("PhoneStudent") + .HasForeignKey("StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -176,14 +173,11 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.Students", b => { - b.Navigation("AddressStudent") - .IsRequired(); + b.Navigation("AddressStudent"); - b.Navigation("EmailAddressStudent") - .IsRequired(); + b.Navigation("EmailAddressStudent"); - b.Navigation("PhoneStudent") - .IsRequired(); + b.Navigation("PhoneStudent"); }); #pragma warning restore 612, 618 } diff --git a/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.cs b/StudentApp/Migrations/20230127094453_UpdateStudentTables.cs similarity index 84% rename from StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.cs rename to StudentApp/Migrations/20230127094453_UpdateStudentTables.cs index 6827570..15a10d5 100644 --- a/StudentApp/Migrations/20230125233033_UpdateStudentsAddressTables.cs +++ b/StudentApp/Migrations/20230127094453_UpdateStudentTables.cs @@ -6,7 +6,7 @@ namespace StudentApp.Migrations { /// - public partial class UpdateStudentsAddressTables : Migration + public partial class UpdateStudentTables : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -15,7 +15,8 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "Student", columns: table => new { - id = table.Column(type: "int", nullable: false, defaultValue: 0), + StudentId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), UserName = table.Column(type: "nvarchar(max)", nullable: false), FirstName = table.Column(type: "nvarchar(max)", nullable: false), SecondName = table.Column(type: "nvarchar(max)", nullable: false), @@ -25,14 +26,14 @@ protected override void Up(MigrationBuilder migrationBuilder) }, constraints: table => { - table.PrimaryKey("PK_Student", x => x.id); + table.PrimaryKey("PK_Student", x => x.StudentId); }); migrationBuilder.CreateTable( name: "StudentAddress", columns: table => new { - Id = table.Column(type: "int", nullable: false) + AddressId = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), Address = table.Column(type: "nvarchar(max)", nullable: false), City = table.Column(type: "nvarchar(max)", nullable: false), @@ -42,12 +43,12 @@ protected override void Up(MigrationBuilder migrationBuilder) }, constraints: table => { - table.PrimaryKey("PK_StudentAddress", x => x.Id); + table.PrimaryKey("PK_StudentAddress", x => x.AddressId); table.ForeignKey( name: "FK_StudentAddress_Student_StudentsId", column: x => x.StudentsId, principalTable: "Student", - principalColumn: "id", + principalColumn: "StudentId", onDelete: ReferentialAction.Cascade); }); @@ -55,19 +56,19 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "StudentEmailAddress", columns: table => new { - Id = table.Column(type: "int", nullable: false) + EmailId = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), EmailAddress = table.Column(type: "nvarchar(max)", nullable: false), StudentsId = table.Column(type: "int", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_StudentEmailAddress", x => x.Id); + table.PrimaryKey("PK_StudentEmailAddress", x => x.EmailId); table.ForeignKey( name: "FK_StudentEmailAddress_Student_StudentsId", column: x => x.StudentsId, principalTable: "Student", - principalColumn: "id", + principalColumn: "StudentId", onDelete: ReferentialAction.Cascade); }); @@ -75,39 +76,36 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "StudentPhoneNo", columns: table => new { - Id = table.Column(type: "int", nullable: false) + PhoneId = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), PhoneNo = table.Column(type: "nvarchar(max)", nullable: false), StudentsId = table.Column(type: "int", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_StudentPhoneNo", x => x.Id); + table.PrimaryKey("PK_StudentPhoneNo", x => x.PhoneId); table.ForeignKey( name: "FK_StudentPhoneNo_Student_StudentsId", column: x => x.StudentsId, principalTable: "Student", - principalColumn: "id", + principalColumn: "StudentId", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "IX_StudentAddress_StudentsId", table: "StudentAddress", - column: "StudentsId", - unique: true); + column: "StudentsId"); migrationBuilder.CreateIndex( name: "IX_StudentEmailAddress_StudentsId", table: "StudentEmailAddress", - column: "StudentsId", - unique: true); + column: "StudentsId"); migrationBuilder.CreateIndex( name: "IX_StudentPhoneNo_StudentsId", table: "StudentPhoneNo", - column: "StudentsId", - unique: true); + column: "StudentsId"); } /// diff --git a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs index 5eda90c..6c93fa6 100644 --- a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs +++ b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs @@ -24,11 +24,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentAddress", b => { - b.Property("Id") + b.Property("AddressId") .ValueGeneratedOnAdd() .HasColumnType("int"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("AddressId")); b.Property("Address") .IsRequired() @@ -48,21 +48,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("StudentsId") .HasColumnType("int"); - b.HasKey("Id"); + b.HasKey("AddressId"); - b.HasIndex("StudentsId") - .IsUnique(); + b.HasIndex("StudentsId"); b.ToTable("StudentAddress"); }); modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => { - b.Property("Id") + b.Property("EmailId") .ValueGeneratedOnAdd() .HasColumnType("int"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("EmailId")); b.Property("EmailAddress") .IsRequired() @@ -71,21 +70,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("StudentsId") .HasColumnType("int"); - b.HasKey("Id"); + b.HasKey("EmailId"); - b.HasIndex("StudentsId") - .IsUnique(); + b.HasIndex("StudentsId"); b.ToTable("StudentEmailAddress"); }); modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => { - b.Property("Id") + b.Property("PhoneId") .ValueGeneratedOnAdd() .HasColumnType("int"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("PhoneId")); b.Property("PhoneNo") .IsRequired() @@ -94,21 +92,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("StudentsId") .HasColumnType("int"); - b.HasKey("Id"); + b.HasKey("PhoneId"); - b.HasIndex("StudentsId") - .IsUnique(); + b.HasIndex("StudentsId"); b.ToTable("StudentPhoneNo"); }); modelBuilder.Entity("StudentApp.Models.Students", b => { - b.Property("Id") + b.Property("StudentId") .ValueGeneratedOnAdd() - .HasColumnType("int") - .HasDefaultValue(0) - .HasColumnName("id"); + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("StudentId")); b.Property("FirstName") .IsRequired() @@ -133,7 +130,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.HasKey("Id"); + b.HasKey("StudentId"); b.ToTable("Student"); }); @@ -141,8 +138,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentAddress", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("AddressStudent") - .HasForeignKey("StudentApp.Models.StudentAddress", "StudentsId") + .WithMany("AddressStudent") + .HasForeignKey("StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -152,8 +149,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentEmailAddress", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("EmailAddressStudent") - .HasForeignKey("StudentApp.Models.StudentEmailAddress", "StudentsId") + .WithMany("EmailAddressStudent") + .HasForeignKey("StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -163,8 +160,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.StudentPhoneNo", b => { b.HasOne("StudentApp.Models.Students", "Students") - .WithOne("PhoneStudent") - .HasForeignKey("StudentApp.Models.StudentPhoneNo", "StudentsId") + .WithMany("PhoneStudent") + .HasForeignKey("StudentsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -173,14 +170,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("StudentApp.Models.Students", b => { - b.Navigation("AddressStudent") - .IsRequired(); + b.Navigation("AddressStudent"); - b.Navigation("EmailAddressStudent") - .IsRequired(); + b.Navigation("EmailAddressStudent"); - b.Navigation("PhoneStudent") - .IsRequired(); + b.Navigation("PhoneStudent"); }); #pragma warning restore 612, 618 } diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index d56dad7..33816e2 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -41,9 +41,9 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) LastName = addStudentRequest.LastName, School = addStudentRequest.School, RegistrationDate = addStudentRequest.RegistrationDate, - PhoneStudent = new PhoneStudentRequest().ToPhoneStudent(addStudentRequest.PhoneStudent), - EmailAddressStudent = new EmailAddressStudentRequest().ToEmailStudent(addStudentRequest.EmailAddressStudent), - AddressStudent = new AddressStudentRequest().ToAddressStudent(addStudentRequest.AddressStudent) + PhoneStudent = (ICollection)new PhoneStudentRequest().ToPhoneStudent(addStudentRequest.PhoneStudent), + EmailAddressStudent = (ICollection)new EmailAddressStudentRequest().ToEmailStudent(addStudentRequest.EmailAddressStudent), + AddressStudent = (ICollection)new AddressStudentRequest().ToAddressStudent(addStudentRequest.AddressStudent) }; } } diff --git a/StudentApp/Models/AddressStudentRequest.cs b/StudentApp/Models/AddressStudentRequest.cs index 28f5d0e..6249dc5 100644 --- a/StudentApp/Models/AddressStudentRequest.cs +++ b/StudentApp/Models/AddressStudentRequest.cs @@ -8,6 +8,10 @@ public class AddressStudentRequest public AddressStudentRequest() { } + + public AddressStudentRequest(ICollection addressStudent) + { + } public AddressStudentRequest(StudentAddress studentAddress) { Address = studentAddress.Address; diff --git a/StudentApp/Models/EmailAddressStudentRequest.cs b/StudentApp/Models/EmailAddressStudentRequest.cs index 3240ce0..50cb48a 100644 --- a/StudentApp/Models/EmailAddressStudentRequest.cs +++ b/StudentApp/Models/EmailAddressStudentRequest.cs @@ -1,5 +1,6 @@  using StudentApp.Controllers.Validations; +using System.ComponentModel.DataAnnotations; namespace StudentApp.Models { @@ -8,11 +9,15 @@ public class EmailAddressStudentRequest public EmailAddressStudentRequest() { } + + public EmailAddressStudentRequest(ICollection emailAddressStudent) + { + } public EmailAddressStudentRequest(StudentEmailAddress studentEmailAddress) { EmailAddress = studentEmailAddress.EmailAddress; } - [IsNotNullOrEmpty] public string EmailAddress { get; private set; } + [IsNotNullOrEmpty] public string EmailAddress { get; set; } public virtual StudentEmailAddress ToEmailStudent(EmailAddressStudentRequest emailAddressStudentRequest) { diff --git a/StudentApp/Models/PhoneStudentRequest.cs b/StudentApp/Models/PhoneStudentRequest.cs index f81b643..7a3d986 100644 --- a/StudentApp/Models/PhoneStudentRequest.cs +++ b/StudentApp/Models/PhoneStudentRequest.cs @@ -8,11 +8,15 @@ public class PhoneStudentRequest public PhoneStudentRequest() { } + + public PhoneStudentRequest(ICollection phoneStudent) + { + } public PhoneStudentRequest(StudentPhoneNo studentPhone) { PhoneNo = studentPhone.PhoneNo; } - [IsNotNullOrEmpty] public string PhoneNo { get; private set; } + [IsNotNullOrEmpty] public string PhoneNo { get; set; } public virtual StudentPhoneNo ToPhoneStudent(PhoneStudentRequest phoneStudentRequest) { diff --git a/StudentApp/Models/StudentResponse.cs b/StudentApp/Models/StudentResponse.cs index ff34f4c..f70e875 100644 --- a/StudentApp/Models/StudentResponse.cs +++ b/StudentApp/Models/StudentResponse.cs @@ -5,7 +5,7 @@ public class StudentResponse : AddStudentRequest { public StudentResponse(Students students) : base(students) { - StudentId = students.Id; + StudentId = students.StudentId; } public int StudentId { get; set;} diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index d14e1e5..0a5ab6b 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -1,44 +1,54 @@  + +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + namespace StudentApp.Models { public class Students { - public int Id { get; set; } + [Key, Column(Order = 0)] + public int StudentId { get; set; } public string UserName { get; set; } public string FirstName { get; set; } public string SecondName { get; set; } public string LastName { get; set; } public string School { get; set; } public DateTime RegistrationDate { get; internal set; } - - public StudentPhoneNo PhoneStudent { get; set; } - public StudentEmailAddress EmailAddressStudent { get; set; } - public StudentAddress AddressStudent { get; set; } + public ICollection PhoneStudent { get; set; } + public ICollection EmailAddressStudent { get; set; } + public ICollection AddressStudent { get; set; } } public class StudentPhoneNo { - public int Id { get; set; } + [Key, Column(Order = 0)] + public int PhoneId { get; set; } public string PhoneNo { get; set; } + [ForeignKey("Students")] public int StudentsId { get; set; } public Students Students { get; set; } } public class StudentEmailAddress { - public int Id { get; set; } + [Key, Column(Order = 0)] + public int EmailId { get; set; } public string EmailAddress { get; set; } + [ForeignKey("Students")] public int StudentsId { get; set; } public Students Students { get; set; } } public class StudentAddress { - public int Id { get; set; } + [Key, Column(Order = 0)] + public int AddressId { get; set; } public string Address { get; set; } public string City { get; set; } public int PostNumber { get; set; } public string Country { get; set; } + [ForeignKey("Students")] public int StudentsId { get; set; } public Students Students { get; set; } } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index e96a5d0..e5bb8e9 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -41,9 +41,9 @@ public virtual Students ToUpdateStudent(UpdateStudentRequest updateStudentReques LastName = updateStudentRequest.LastName, School = updateStudentRequest.School, RegistrationDate = updateStudentRequest.RegistrationDate, - PhoneStudent = new PhoneStudentRequest().ToPhoneStudent(updateStudentRequest.PhoneStudent), - EmailAddressStudent = new EmailAddressStudentRequest().ToEmailStudent(updateStudentRequest.EmailAddressStudent), - AddressStudent = new AddressStudentRequest().ToAddressStudent(updateStudentRequest.AddressStudent) + PhoneStudent = (ICollection)new PhoneStudentRequest().ToPhoneStudent(updateStudentRequest.PhoneStudent), + EmailAddressStudent = (ICollection)new EmailAddressStudentRequest().ToEmailStudent(updateStudentRequest.EmailAddressStudent), + AddressStudent = (ICollection)new AddressStudentRequest().ToAddressStudent(updateStudentRequest.AddressStudent) }; } } diff --git a/StudentApp/Repo/Repo.cs b/StudentApp/Repo/Repo.cs index 86a5cce..7d35f43 100644 --- a/StudentApp/Repo/Repo.cs +++ b/StudentApp/Repo/Repo.cs @@ -8,6 +8,7 @@ namespace StudentApp.Repo; public class Repo : IRepo { private readonly StudentAppContext _context; + public Repo(StudentAppContext context) { @@ -18,7 +19,7 @@ public async Task> Get() => await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).ToListAsync(); public async Task GetAsId(int id) => - await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); + await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.StudentId == id); public async Task AddStudent(Students students) { @@ -29,7 +30,7 @@ public async Task AddStudent(Students students) public async Task UpdateStudent(int id, Students students) { - var result = await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.Id == id); + var result = await _context.Student.Include(s => s.PhoneStudent).Include(s => s.EmailAddressStudent).Include(s => s.AddressStudent).FirstOrDefaultAsync(s => s.StudentId == id); if (result != null) { result.UserName = students.UserName; diff --git a/StudentApp/Views/Students/Delete.cshtml b/StudentApp/Views/Students/Delete.cshtml index 83dfdb1..b26898a 100644 --- a/StudentApp/Views/Students/Delete.cshtml +++ b/StudentApp/Views/Students/Delete.cshtml @@ -44,7 +44,7 @@
- + | Back to List
diff --git a/StudentApp/Views/Students/Details.cshtml b/StudentApp/Views/Students/Details.cshtml index e497327..4a84eb2 100644 --- a/StudentApp/Views/Students/Details.cshtml +++ b/StudentApp/Views/Students/Details.cshtml @@ -43,6 +43,6 @@
diff --git a/StudentApp/Views/Students/Edit.cshtml b/StudentApp/Views/Students/Edit.cshtml index 972265d..31097f7 100644 --- a/StudentApp/Views/Students/Edit.cshtml +++ b/StudentApp/Views/Students/Edit.cshtml @@ -12,7 +12,7 @@
- +
diff --git a/StudentApp/Views/Students/Index.cshtml b/StudentApp/Views/Students/Index.cshtml index aaa1208..337b98d 100644 --- a/StudentApp/Views/Students/Index.cshtml +++ b/StudentApp/Views/Students/Index.cshtml @@ -49,9 +49,9 @@ @Html.DisplayFor(modelItem => item.School) - Edit | - Details | - Delete + Edit | + Details | + Delete } From 0c4a10d601106a0ae7cb08933ade2b52fb9124fb Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Tue, 31 Jan 2023 00:02:40 +0100 Subject: [PATCH 19/69] ICollection, List --- StudentApp/Controllers/StudentsController.cs | 4 ++-- StudentApp/Models/AddStudentRequest.cs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/StudentApp/Controllers/StudentsController.cs b/StudentApp/Controllers/StudentsController.cs index e8d3bc3..9130e2a 100644 --- a/StudentApp/Controllers/StudentsController.cs +++ b/StudentApp/Controllers/StudentsController.cs @@ -42,12 +42,12 @@ public async Task> GetAsId(int id) } [HttpPost] - public async Task> AddStudent(AddStudentRequest addStudentRequest) + public async Task> AddStudent(AddStudentRequest addStudentRequest) { var request = new AddStudentRequest(); var student = request.ToStudent(addStudentRequest); var result = await _service.AddStudent(student); - return (ICollection)Ok(new StudentResponse(result)); + return Ok(new StudentResponse(result)); } [HttpPut] diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index 33816e2..c940058 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -16,9 +16,9 @@ public AddStudentRequest(Students students) LastName= students.LastName; School= students.School; RegistrationDate = students.RegistrationDate; - PhoneStudent = new PhoneStudentRequest(students.PhoneStudent); - EmailAddressStudent = new EmailAddressStudentRequest(students.EmailAddressStudent); - AddressStudent = new AddressStudentRequest(students.AddressStudent); + PhoneStudent = students.PhoneStudent.Select(p => new PhoneStudentRequest(p)).ToList(); + EmailAddressStudent = students.EmailAddressStudent.Select(p => new EmailAddressStudentRequest(p)).ToList(); + AddressStudent = students.AddressStudent.Select(p => new AddressStudentRequest(p)).ToList(); } [IsNotNullOrEmpty] public string UserName { get; set; } @@ -27,9 +27,9 @@ public AddStudentRequest(Students students) [IsNotNullOrEmpty] public string LastName { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public PhoneStudentRequest PhoneStudent { get; set; } - [IsNotNullOrEmpty] public EmailAddressStudentRequest EmailAddressStudent { get; set; } - [IsNotNullOrEmpty] public AddressStudentRequest AddressStudent { get; set; } + [IsNotNullOrEmpty] public ICollection PhoneStudent { get; set; } + [IsNotNullOrEmpty] public ICollection EmailAddressStudent { get; set; } + [IsNotNullOrEmpty] public ICollection AddressStudent { get; set; } public virtual Students ToStudent(AddStudentRequest addStudentRequest) { @@ -41,9 +41,9 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) LastName = addStudentRequest.LastName, School = addStudentRequest.School, RegistrationDate = addStudentRequest.RegistrationDate, - PhoneStudent = (ICollection)new PhoneStudentRequest().ToPhoneStudent(addStudentRequest.PhoneStudent), - EmailAddressStudent = (ICollection)new EmailAddressStudentRequest().ToEmailStudent(addStudentRequest.EmailAddressStudent), - AddressStudent = (ICollection)new AddressStudentRequest().ToAddressStudent(addStudentRequest.AddressStudent) + PhoneStudent = (ICollection)addStudentRequest.PhoneStudent.Select(p => new PhoneStudentRequest()), + EmailAddressStudent = (ICollection)addStudentRequest.EmailAddressStudent.Select(p => new EmailAddressStudentRequest()), + AddressStudent = (ICollection)addStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()) }; } } From 5c7587d4d46ffa3c5ab487be22e2c3fc20ea5a3a Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Sat, 4 Feb 2023 14:00:51 +0100 Subject: [PATCH 20/69] StudentAppModelsUpdate --- StudentApp/Controllers/StudentsController.cs | 6 +++--- StudentApp/Models/AddStudentRequest.cs | 3 ++- .../Models/EmailAddressStudentRequest.cs | 1 - StudentApp/Models/UpdateStudentRequest.cs | 18 +++++++++--------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/StudentApp/Controllers/StudentsController.cs b/StudentApp/Controllers/StudentsController.cs index 9130e2a..d55220f 100644 --- a/StudentApp/Controllers/StudentsController.cs +++ b/StudentApp/Controllers/StudentsController.cs @@ -52,15 +52,15 @@ public async Task> AddStudent(AddStudentRequest ad [HttpPut] [Route("{id}")] - public async Task> UpdateStudent(int id, UpdateStudentRequest updateStudentRequest) + public async Task> UpdateStudent(int id, UpdateStudentRequest updateStudentRequest) { var request = new UpdateStudentRequest(); var student = request.ToUpdateStudent(updateStudentRequest); var result = await _service.UpdateStudent(id, student); if (result == null) - return (ICollection)NotFound($"Wrong Id {id}"); - return (ICollection)Ok(new StudentResponse(result)); + return NotFound($"Wrong Id {id}"); + return Ok(new StudentResponse(result)); } [HttpDelete] diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index c940058..e610a24 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -1,5 +1,6 @@  using StudentApp.Controllers.Validations; +using System.Linq; namespace StudentApp.Models { @@ -43,7 +44,7 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) RegistrationDate = addStudentRequest.RegistrationDate, PhoneStudent = (ICollection)addStudentRequest.PhoneStudent.Select(p => new PhoneStudentRequest()), EmailAddressStudent = (ICollection)addStudentRequest.EmailAddressStudent.Select(p => new EmailAddressStudentRequest()), - AddressStudent = (ICollection)addStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()) + AddressStudent = (ICollection)addStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()), }; } } diff --git a/StudentApp/Models/EmailAddressStudentRequest.cs b/StudentApp/Models/EmailAddressStudentRequest.cs index 50cb48a..04ba054 100644 --- a/StudentApp/Models/EmailAddressStudentRequest.cs +++ b/StudentApp/Models/EmailAddressStudentRequest.cs @@ -1,6 +1,5 @@  using StudentApp.Controllers.Validations; -using System.ComponentModel.DataAnnotations; namespace StudentApp.Models { diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index e5bb8e9..7b89e0e 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -15,9 +15,9 @@ public UpdateStudentRequest(Students students) LastName = students.LastName; School = students.School; RegistrationDate = students.RegistrationDate; - PhoneStudent = new PhoneStudentRequest(students.PhoneStudent); - EmailAddressStudent = new EmailAddressStudentRequest(students.EmailAddressStudent); - AddressStudent = new AddressStudentRequest(students.AddressStudent); + PhoneStudent = students.PhoneStudent.Select(p => new PhoneStudentRequest(p)).ToList(); + EmailAddressStudent = students.EmailAddressStudent.Select(p => new EmailAddressStudentRequest(p)).ToList(); + AddressStudent = students.AddressStudent.Select(p => new AddressStudentRequest(p)).ToList(); } [IsNotNullOrEmpty] public string UserName { get; set; } @@ -26,9 +26,9 @@ public UpdateStudentRequest(Students students) [IsNotNullOrEmpty] public string LastName { get; set; } [IsNotNullOrEmpty] public string School { get; set; } [ValidateUpdateJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public PhoneStudentRequest PhoneStudent { get; set; } - [IsNotNullOrEmpty] public EmailAddressStudentRequest EmailAddressStudent { get; set; } - [IsNotNullOrEmpty] public AddressStudentRequest AddressStudent { get; set; } + [IsNotNullOrEmpty] public ICollection PhoneStudent { get; set; } + [IsNotNullOrEmpty] public ICollection EmailAddressStudent { get; set; } + [IsNotNullOrEmpty] public ICollection AddressStudent { get; set; } public virtual Students ToUpdateStudent(UpdateStudentRequest updateStudentRequest) @@ -41,9 +41,9 @@ public virtual Students ToUpdateStudent(UpdateStudentRequest updateStudentReques LastName = updateStudentRequest.LastName, School = updateStudentRequest.School, RegistrationDate = updateStudentRequest.RegistrationDate, - PhoneStudent = (ICollection)new PhoneStudentRequest().ToPhoneStudent(updateStudentRequest.PhoneStudent), - EmailAddressStudent = (ICollection)new EmailAddressStudentRequest().ToEmailStudent(updateStudentRequest.EmailAddressStudent), - AddressStudent = (ICollection)new AddressStudentRequest().ToAddressStudent(updateStudentRequest.AddressStudent) + PhoneStudent = (ICollection)updateStudentRequest.PhoneStudent.Select(p => new PhoneStudentRequest()), + EmailAddressStudent = (ICollection)updateStudentRequest.EmailAddressStudent.Select(p => new EmailAddressStudentRequest()), + AddressStudent = (ICollection)updateStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()), }; } } From 79a044e899cafc183ecfb8b89d6d9a660322c218 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Fri, 3 Mar 2023 12:27:44 +0100 Subject: [PATCH 21/69] Index&Layout --- StudentApp/Models/AddStudentRequest.cs | 1 - StudentApp/Views/Home/Index.cshtml | 2 +- StudentApp/Views/Shared/_Layout.cshtml | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index e610a24..d01f36e 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -1,6 +1,5 @@  using StudentApp.Controllers.Validations; -using System.Linq; namespace StudentApp.Models { diff --git a/StudentApp/Views/Home/Index.cshtml b/StudentApp/Views/Home/Index.cshtml index d2d19bd..f54bff2 100644 --- a/StudentApp/Views/Home/Index.cshtml +++ b/StudentApp/Views/Home/Index.cshtml @@ -4,5 +4,5 @@

Welcome

-

Learn about building Web apps with ASP.NET Core.

+

Learn about building Web apps with ASP.NET Core.

diff --git a/StudentApp/Views/Shared/_Layout.cshtml b/StudentApp/Views/Shared/_Layout.cshtml index d1336ec..5f1769e 100644 --- a/StudentApp/Views/Shared/_Layout.cshtml +++ b/StudentApp/Views/Shared/_Layout.cshtml @@ -41,7 +41,7 @@ From f62d2cba8478da88bd90e0db3b44d1877f46379f Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Sun, 5 Mar 2023 21:55:53 +0100 Subject: [PATCH 22/69] BeforeAuthentication --- StudentApp/Controllers/StudentsController.cs | 4 ++-- StudentApp/Program.cs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/StudentApp/Controllers/StudentsController.cs b/StudentApp/Controllers/StudentsController.cs index d55220f..c48eaa3 100644 --- a/StudentApp/Controllers/StudentsController.cs +++ b/StudentApp/Controllers/StudentsController.cs @@ -7,8 +7,8 @@ namespace StudentApp.Controllers; -[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")] -[Authorize] +//[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")] +//[Authorize] [ApiController] [Route("[controller]")] public class StudentsController : ControllerBase diff --git a/StudentApp/Program.cs b/StudentApp/Program.cs index 72ea632..45c4003 100644 --- a/StudentApp/Program.cs +++ b/StudentApp/Program.cs @@ -13,8 +13,8 @@ builder.Services.AddControllers(); builder.Services.AddDbContext(option => option.UseSqlServer(builder.Configuration.GetConnectionString("StudentAppContext") ?? throw new InvalidOperationException("Connection string 'StudentAppContext' not found."))); -builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) - .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")); +//builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) +// .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")); #region AddingServicesToTheContainer builder.Services.AddControllersWithViews(); @@ -31,9 +31,9 @@ #region SecureApi builder.Services.AddSwaggerGen( - c=> + c => { - c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title ="Swagger Azure AD Demo", Version= "v1" }); + c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Swagger Azure AD Demo", Version = "v1" }); c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { Description = "Oauth2.0 which uses AuthorizationCode flow", @@ -81,7 +81,7 @@ }); app.UseHttpsRedirection(); -app.UseAuthentication(); +//app.UseAuthentication(); app.UseAuthorization(); app.MapControllerRoute( From f064405320359b14db60df5ff726d9d671344014 Mon Sep 17 00:00:00 2001 From: Omer Kurtuldu Date: Tue, 7 Mar 2023 22:09:44 +0100 Subject: [PATCH 23/69] Update requests --- StudentApp/Models/AddStudentRequest.cs | 6 +++--- StudentApp/Models/AddressStudentRequest.cs | 11 +++++++++++ StudentApp/Models/EmailAddressStudentRequest.cs | 2 ++ StudentApp/Models/PhoneStudentRequest.cs | 2 ++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index e610a24..f2530b2 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -42,9 +42,9 @@ public virtual Students ToStudent(AddStudentRequest addStudentRequest) LastName = addStudentRequest.LastName, School = addStudentRequest.School, RegistrationDate = addStudentRequest.RegistrationDate, - PhoneStudent = (ICollection)addStudentRequest.PhoneStudent.Select(p => new PhoneStudentRequest()), - EmailAddressStudent = (ICollection)addStudentRequest.EmailAddressStudent.Select(p => new EmailAddressStudentRequest()), - AddressStudent = (ICollection)addStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()), + PhoneStudent = addStudentRequest.PhoneStudent.Select(p => p.ToPhoneStudent()).ToList(), + EmailAddressStudent = addStudentRequest.EmailAddressStudent.Select(e => e.ToEmailStudent()).ToList(), + AddressStudent = addStudentRequest.AddressStudent.Select(p => p.ToAddressStudent()).ToList(), }; } } diff --git a/StudentApp/Models/AddressStudentRequest.cs b/StudentApp/Models/AddressStudentRequest.cs index 6249dc5..28784d8 100644 --- a/StudentApp/Models/AddressStudentRequest.cs +++ b/StudentApp/Models/AddressStudentRequest.cs @@ -35,5 +35,16 @@ public virtual StudentAddress ToAddressStudent(AddressStudentRequest addressStud Country = addressStudentRequest.Country }; } + + public StudentAddress ToAddressStudent() + { + return new StudentAddress + { + Address= Address, + City = City, + PostNumber = PostNumber, + Country = Country + }; + } } } diff --git a/StudentApp/Models/EmailAddressStudentRequest.cs b/StudentApp/Models/EmailAddressStudentRequest.cs index 04ba054..203ec7d 100644 --- a/StudentApp/Models/EmailAddressStudentRequest.cs +++ b/StudentApp/Models/EmailAddressStudentRequest.cs @@ -25,5 +25,7 @@ public virtual StudentEmailAddress ToEmailStudent(EmailAddressStudentRequest ema EmailAddress = emailAddressStudentRequest.EmailAddress }; } + + public StudentEmailAddress ToEmailStudent() => new StudentEmailAddress { EmailAddress = EmailAddress }; } } diff --git a/StudentApp/Models/PhoneStudentRequest.cs b/StudentApp/Models/PhoneStudentRequest.cs index 7a3d986..5ebb068 100644 --- a/StudentApp/Models/PhoneStudentRequest.cs +++ b/StudentApp/Models/PhoneStudentRequest.cs @@ -25,5 +25,7 @@ public virtual StudentPhoneNo ToPhoneStudent(PhoneStudentRequest phoneStudentReq PhoneNo = phoneStudentRequest.PhoneNo, }; } + + public StudentPhoneNo ToPhoneStudent() => new StudentPhoneNo { PhoneNo = PhoneNo }; } } From 752292ae2f144f998a641bbbd3e9a4f967eba4b5 Mon Sep 17 00:00:00 2001 From: Ahmet Ulucay <57094137+ahmetulucay@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:23:50 +0100 Subject: [PATCH 24/69] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef71c7d..df3b8a5 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ STEPS (Swagger Azure AD Autht - Swagger Azure AD Login)= ******************************************************** ![Authorized Page](https://user-images.githubusercontent.com/57094137/210284591-4a4d7eaa-275f-49ea-bf0a-a87bdf80f35f.jpg) -Opening programs = --------------------------------------- +Open programs = +----------------------------------- Open Visual Studio/-Code. After that Open your project/api (Exp.=StudentApp) From 5f7764705c51d3ce73702507c0ccf1d81ef857a7 Mon Sep 17 00:00:00 2001 From: Ahmet U <57094137+GitAhmet@users.noreply.github.com> Date: Thu, 9 Mar 2023 10:52:49 +0100 Subject: [PATCH 25/69] RecreatingContext --- ...1_UpdateStudentsAddressTables.Designer.cs} | 16 ++-- ...0308211741_UpdateStudentsAddressTables.cs} | 11 +-- .../StudentAppContextModelSnapshot.cs | 12 +-- StudentApp/Models/AddStudentRequest.cs | 79 +++++++++---------- StudentApp/Models/AddressStudentRequest.cs | 66 +++++++++------- .../Models/EmailAddressStudentRequest.cs | 39 ++++----- StudentApp/Models/PhoneStudentRequest.cs | 39 ++++----- StudentApp/Models/Students.cs | 12 ++- StudentApp/Models/UpdateStudentRequest.cs | 9 ++- StudentApp/StudentApp.csproj | 4 - StudentApp/Views/Shared/_Layout.cshtml | 4 +- 11 files changed, 145 insertions(+), 146 deletions(-) rename StudentApp/Migrations/{20230127094453_UpdateStudentTables.Designer.cs => 20230308211741_UpdateStudentsAddressTables.Designer.cs} (91%) rename StudentApp/Migrations/{20230127094453_UpdateStudentTables.cs => 20230308211741_UpdateStudentsAddressTables.cs} (92%) diff --git a/StudentApp/Migrations/20230127094453_UpdateStudentTables.Designer.cs b/StudentApp/Migrations/20230308211741_UpdateStudentsAddressTables.Designer.cs similarity index 91% rename from StudentApp/Migrations/20230127094453_UpdateStudentTables.Designer.cs rename to StudentApp/Migrations/20230308211741_UpdateStudentsAddressTables.Designer.cs index fbb7eb8..978eea1 100644 --- a/StudentApp/Migrations/20230127094453_UpdateStudentTables.Designer.cs +++ b/StudentApp/Migrations/20230308211741_UpdateStudentsAddressTables.Designer.cs @@ -12,8 +12,8 @@ namespace StudentApp.Migrations { [DbContext(typeof(StudentAppContext))] - [Migration("20230127094453_UpdateStudentTables")] - partial class UpdateStudentTables + [Migration("20230308211741_UpdateStudentsAddressTables")] + partial class UpdateStudentsAddressTables { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -142,9 +142,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) { b.HasOne("StudentApp.Models.Students", "Students") .WithMany("AddressStudent") - .HasForeignKey("StudentsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("StudentsId"); b.Navigation("Students"); }); @@ -153,9 +151,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) { b.HasOne("StudentApp.Models.Students", "Students") .WithMany("EmailAddressStudent") - .HasForeignKey("StudentsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("StudentsId"); b.Navigation("Students"); }); @@ -164,9 +160,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) { b.HasOne("StudentApp.Models.Students", "Students") .WithMany("PhoneStudent") - .HasForeignKey("StudentsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("StudentsId"); b.Navigation("Students"); }); diff --git a/StudentApp/Migrations/20230127094453_UpdateStudentTables.cs b/StudentApp/Migrations/20230308211741_UpdateStudentsAddressTables.cs similarity index 92% rename from StudentApp/Migrations/20230127094453_UpdateStudentTables.cs rename to StudentApp/Migrations/20230308211741_UpdateStudentsAddressTables.cs index 15a10d5..9b340ba 100644 --- a/StudentApp/Migrations/20230127094453_UpdateStudentTables.cs +++ b/StudentApp/Migrations/20230308211741_UpdateStudentsAddressTables.cs @@ -6,7 +6,7 @@ namespace StudentApp.Migrations { /// - public partial class UpdateStudentTables : Migration + public partial class UpdateStudentsAddressTables : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -48,8 +48,7 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "FK_StudentAddress_Student_StudentsId", column: x => x.StudentsId, principalTable: "Student", - principalColumn: "StudentId", - onDelete: ReferentialAction.Cascade); + principalColumn: "StudentId"); }); migrationBuilder.CreateTable( @@ -68,8 +67,7 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "FK_StudentEmailAddress_Student_StudentsId", column: x => x.StudentsId, principalTable: "Student", - principalColumn: "StudentId", - onDelete: ReferentialAction.Cascade); + principalColumn: "StudentId"); }); migrationBuilder.CreateTable( @@ -88,8 +86,7 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "FK_StudentPhoneNo_Student_StudentsId", column: x => x.StudentsId, principalTable: "Student", - principalColumn: "StudentId", - onDelete: ReferentialAction.Cascade); + principalColumn: "StudentId"); }); migrationBuilder.CreateIndex( diff --git a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs index 6c93fa6..12bb0fb 100644 --- a/StudentApp/Migrations/StudentAppContextModelSnapshot.cs +++ b/StudentApp/Migrations/StudentAppContextModelSnapshot.cs @@ -139,9 +139,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.HasOne("StudentApp.Models.Students", "Students") .WithMany("AddressStudent") - .HasForeignKey("StudentsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("StudentsId"); b.Navigation("Students"); }); @@ -150,9 +148,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.HasOne("StudentApp.Models.Students", "Students") .WithMany("EmailAddressStudent") - .HasForeignKey("StudentsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("StudentsId"); b.Navigation("Students"); }); @@ -161,9 +157,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.HasOne("StudentApp.Models.Students", "Students") .WithMany("PhoneStudent") - .HasForeignKey("StudentsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("StudentsId"); b.Navigation("Students"); }); diff --git a/StudentApp/Models/AddStudentRequest.cs b/StudentApp/Models/AddStudentRequest.cs index d01f36e..080e45f 100644 --- a/StudentApp/Models/AddStudentRequest.cs +++ b/StudentApp/Models/AddStudentRequest.cs @@ -1,50 +1,49 @@  using StudentApp.Controllers.Validations; -namespace StudentApp.Models +namespace StudentApp.Models; + +public class AddStudentRequest { - public class AddStudentRequest + public AddStudentRequest() { - public AddStudentRequest() - { - } - public AddStudentRequest(Students students) - { - UserName= students.UserName??"Test"; - FirstName= students.FirstName; - SecondName = students.SecondName; - LastName= students.LastName; - School= students.School; - RegistrationDate = students.RegistrationDate; - PhoneStudent = students.PhoneStudent.Select(p => new PhoneStudentRequest(p)).ToList(); - EmailAddressStudent = students.EmailAddressStudent.Select(p => new EmailAddressStudentRequest(p)).ToList(); - AddressStudent = students.AddressStudent.Select(p => new AddressStudentRequest(p)).ToList(); - } + } + public AddStudentRequest(Students students) + { + UserName = students.UserName ?? "Test"; + FirstName = students.FirstName; + SecondName = students.SecondName; + LastName = students.LastName; + School = students.School; + RegistrationDate = students.RegistrationDate; + PhoneStudent = students.PhoneStudent.Select(p => new PhoneStudentRequest(p)).ToList(); + EmailAddressStudent = students.EmailAddressStudent.Select(p => new EmailAddressStudentRequest(p)).ToList(); + AddressStudent = students.AddressStudent.Select(p => new AddressStudentRequest(p)).ToList(); + } - [IsNotNullOrEmpty] public string UserName { get; set; } - [IsNotNullOrEmpty] public string FirstName { get; set; } - public string SecondName { get; set; } - [IsNotNullOrEmpty] public string LastName { get; set; } - [IsNotNullOrEmpty] public string School { get; set; } - [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } - [IsNotNullOrEmpty] public ICollection PhoneStudent { get; set; } - [IsNotNullOrEmpty] public ICollection EmailAddressStudent { get; set; } - [IsNotNullOrEmpty] public ICollection AddressStudent { get; set; } + [IsNotNullOrEmpty] public string UserName { get; set; } + [IsNotNullOrEmpty] public string FirstName { get; set; } + public string SecondName { get; set; } + [IsNotNullOrEmpty] public string LastName { get; set; } + [IsNotNullOrEmpty] public string School { get; set; } + [ValidateAddJoinDate] public DateTime RegistrationDate { get; set; } + [IsNotNullOrEmpty] public ICollection PhoneStudent { get; set; } + [IsNotNullOrEmpty] public ICollection EmailAddressStudent { get; set; } + [IsNotNullOrEmpty] public ICollection AddressStudent { get; set; } - public virtual Students ToStudent(AddStudentRequest addStudentRequest) + public virtual Students ToStudent(AddStudentRequest addStudentRequest) + { + return new Students { - return new Students - { - UserName = addStudentRequest.UserName, - FirstName = addStudentRequest.FirstName, - SecondName = addStudentRequest.SecondName, - LastName = addStudentRequest.LastName, - School = addStudentRequest.School, - RegistrationDate = addStudentRequest.RegistrationDate, - PhoneStudent = (ICollection)addStudentRequest.PhoneStudent.Select(p => new PhoneStudentRequest()), - EmailAddressStudent = (ICollection)addStudentRequest.EmailAddressStudent.Select(p => new EmailAddressStudentRequest()), - AddressStudent = (ICollection)addStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()), - }; - } + UserName = addStudentRequest.UserName, + FirstName = addStudentRequest.FirstName, + SecondName = addStudentRequest.SecondName, + LastName = addStudentRequest.LastName, + School = addStudentRequest.School, + RegistrationDate = addStudentRequest.RegistrationDate, + PhoneStudent = addStudentRequest.PhoneStudent.Select(p => p.ToPhoneStudent()).ToList(), + EmailAddressStudent = addStudentRequest.EmailAddressStudent.Select(e => e.ToEmailStudent()).ToList(), + AddressStudent = addStudentRequest.AddressStudent.Select(p => p.ToAddressStudent()).ToList(), + }; } } diff --git a/StudentApp/Models/AddressStudentRequest.cs b/StudentApp/Models/AddressStudentRequest.cs index 6249dc5..754de27 100644 --- a/StudentApp/Models/AddressStudentRequest.cs +++ b/StudentApp/Models/AddressStudentRequest.cs @@ -1,39 +1,49 @@  using StudentApp.Controllers.Validations; -namespace StudentApp.Models +namespace StudentApp.Models; + +public class AddressStudentRequest { - public class AddressStudentRequest + public AddressStudentRequest() { - public AddressStudentRequest() - { - } + } - public AddressStudentRequest(ICollection addressStudent) - { - } - public AddressStudentRequest(StudentAddress studentAddress) - { - Address = studentAddress.Address; - City = studentAddress.City; - PostNumber = studentAddress.PostNumber; - Country= studentAddress.Country; - } + public AddressStudentRequest(ICollection addressStudent) + { + } + public AddressStudentRequest(StudentAddress studentAddress) + { + Address = studentAddress.Address; + City = studentAddress.City; + PostNumber = studentAddress.PostNumber; + Country = studentAddress.Country; + } - [IsNotNullOrEmpty] public string Address{ get; set; } - [IsNotNullOrEmpty] public string City { get; set; } - [IsNotNullOrEmpty] public int PostNumber { get; set; } - [IsNotNullOrEmpty] public string Country { get; set; } + [IsNotNullOrEmpty] public string Address { get; set; } + [IsNotNullOrEmpty] public string City { get; set; } + [IsNotNullOrEmpty] public int PostNumber { get; set; } + [IsNotNullOrEmpty] public string Country { get; set; } - public virtual StudentAddress ToAddressStudent(AddressStudentRequest addressStudentRequest) + public virtual StudentAddress ToAddressStudent(AddressStudentRequest addressStudentRequest) + { + return new StudentAddress + { + Address = addressStudentRequest.Address, + City = addressStudentRequest.City, + PostNumber = addressStudentRequest.PostNumber, + Country = addressStudentRequest.Country + }; + } + + public StudentAddress ToAddressStudent() + { + return new StudentAddress { - return new StudentAddress - { - Address= addressStudentRequest.Address, - City = addressStudentRequest.City, - PostNumber = addressStudentRequest.PostNumber, - Country = addressStudentRequest.Country - }; - } + Address = Address, + City = City, + PostNumber = PostNumber, + Country = Country + }; } } diff --git a/StudentApp/Models/EmailAddressStudentRequest.cs b/StudentApp/Models/EmailAddressStudentRequest.cs index 04ba054..e11e717 100644 --- a/StudentApp/Models/EmailAddressStudentRequest.cs +++ b/StudentApp/Models/EmailAddressStudentRequest.cs @@ -1,29 +1,30 @@  using StudentApp.Controllers.Validations; -namespace StudentApp.Models +namespace StudentApp.Models; + +public class EmailAddressStudentRequest { - public class EmailAddressStudentRequest + public EmailAddressStudentRequest() { - public EmailAddressStudentRequest() - { - } + } - public EmailAddressStudentRequest(ICollection emailAddressStudent) - { - } - public EmailAddressStudentRequest(StudentEmailAddress studentEmailAddress) - { - EmailAddress = studentEmailAddress.EmailAddress; - } - [IsNotNullOrEmpty] public string EmailAddress { get; set; } + public EmailAddressStudentRequest(ICollection emailAddressStudent) + { + } + public EmailAddressStudentRequest(StudentEmailAddress studentEmailAddress) + { + EmailAddress = studentEmailAddress.EmailAddress; + } + [IsNotNullOrEmpty] public string EmailAddress { get; set; } - public virtual StudentEmailAddress ToEmailStudent(EmailAddressStudentRequest emailAddressStudentRequest) + public virtual StudentEmailAddress ToEmailStudent(EmailAddressStudentRequest emailAddressStudentRequest) + { + return new StudentEmailAddress { - return new StudentEmailAddress - { - EmailAddress = emailAddressStudentRequest.EmailAddress - }; - } + EmailAddress = emailAddressStudentRequest.EmailAddress + }; } + + public StudentEmailAddress ToEmailStudent() => new StudentEmailAddress { EmailAddress = EmailAddress }; } diff --git a/StudentApp/Models/PhoneStudentRequest.cs b/StudentApp/Models/PhoneStudentRequest.cs index 7a3d986..efd1a7c 100644 --- a/StudentApp/Models/PhoneStudentRequest.cs +++ b/StudentApp/Models/PhoneStudentRequest.cs @@ -1,29 +1,30 @@  using StudentApp.Controllers.Validations; -namespace StudentApp.Models +namespace StudentApp.Models; + +public class PhoneStudentRequest { - public class PhoneStudentRequest + public PhoneStudentRequest() { - public PhoneStudentRequest() - { - } + } - public PhoneStudentRequest(ICollection phoneStudent) - { - } - public PhoneStudentRequest(StudentPhoneNo studentPhone) - { - PhoneNo = studentPhone.PhoneNo; - } - [IsNotNullOrEmpty] public string PhoneNo { get; set; } + public PhoneStudentRequest(ICollection phoneStudent) + { + } + public PhoneStudentRequest(StudentPhoneNo studentPhone) + { + PhoneNo = studentPhone.PhoneNo; + } + [IsNotNullOrEmpty] public string PhoneNo { get; set; } - public virtual StudentPhoneNo ToPhoneStudent(PhoneStudentRequest phoneStudentRequest) + public virtual StudentPhoneNo ToPhoneStudent(PhoneStudentRequest phoneStudentRequest) + { + return new StudentPhoneNo { - return new StudentPhoneNo - { - PhoneNo = phoneStudentRequest.PhoneNo, - }; - } + PhoneNo = phoneStudentRequest.PhoneNo, + }; } + + public StudentPhoneNo ToPhoneStudent() => new StudentPhoneNo { PhoneNo = PhoneNo }; } diff --git a/StudentApp/Models/Students.cs b/StudentApp/Models/Students.cs index 0a5ab6b..2552363 100644 --- a/StudentApp/Models/Students.cs +++ b/StudentApp/Models/Students.cs @@ -7,7 +7,8 @@ namespace StudentApp.Models { public class Students { - [Key, Column(Order = 0)] + //[Key, Column(Order = 0)] + [Key] public int StudentId { get; set; } public string UserName { get; set; } public string FirstName { get; set; } @@ -22,7 +23,8 @@ public class Students public class StudentPhoneNo { - [Key, Column(Order = 0)] + //[Key, Column(Order = 0)] + [Key] public int PhoneId { get; set; } public string PhoneNo { get; set; } [ForeignKey("Students")] @@ -32,7 +34,8 @@ public class StudentPhoneNo public class StudentEmailAddress { - [Key, Column(Order = 0)] + //[Key, Column(Order = 0)] + [Key] public int EmailId { get; set; } public string EmailAddress { get; set; } [ForeignKey("Students")] @@ -42,7 +45,8 @@ public class StudentEmailAddress } public class StudentAddress { - [Key, Column(Order = 0)] + //[Key, Column(Order = 0)] + [Key] public int AddressId { get; set; } public string Address { get; set; } public string City { get; set; } diff --git a/StudentApp/Models/UpdateStudentRequest.cs b/StudentApp/Models/UpdateStudentRequest.cs index 7b89e0e..966c2b8 100644 --- a/StudentApp/Models/UpdateStudentRequest.cs +++ b/StudentApp/Models/UpdateStudentRequest.cs @@ -41,9 +41,12 @@ public virtual Students ToUpdateStudent(UpdateStudentRequest updateStudentReques LastName = updateStudentRequest.LastName, School = updateStudentRequest.School, RegistrationDate = updateStudentRequest.RegistrationDate, - PhoneStudent = (ICollection)updateStudentRequest.PhoneStudent.Select(p => new PhoneStudentRequest()), - EmailAddressStudent = (ICollection)updateStudentRequest.EmailAddressStudent.Select(p => new EmailAddressStudentRequest()), - AddressStudent = (ICollection)updateStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()), + //PhoneStudent = (ICollection)updateStudentRequest.PhoneStudent.Select(p => new PhoneStudentRequest()), + //EmailAddressStudent = (ICollection)updateStudentRequest.EmailAddressStudent.Select(p => new EmailAddressStudentRequest()), + //AddressStudent = (ICollection)updateStudentRequest.AddressStudent.Select(p => new AddressStudentRequest()), + PhoneStudent = updateStudentRequest.PhoneStudent.Select(p => p.ToPhoneStudent()).ToList(), + EmailAddressStudent = updateStudentRequest.EmailAddressStudent.Select(e => e.ToEmailStudent()).ToList(), + AddressStudent = updateStudentRequest.AddressStudent.Select(p => p.ToAddressStudent()).ToList(), }; } } diff --git a/StudentApp/StudentApp.csproj b/StudentApp/StudentApp.csproj index ce76fc0..27d1d24 100644 --- a/StudentApp/StudentApp.csproj +++ b/StudentApp/StudentApp.csproj @@ -33,10 +33,6 @@ - - - - diff --git a/StudentApp/Views/Shared/_Layout.cshtml b/StudentApp/Views/Shared/_Layout.cshtml index 5f1769e..6083aa9 100644 --- a/StudentApp/Views/Shared/_Layout.cshtml +++ b/StudentApp/Views/Shared/_Layout.cshtml @@ -13,10 +13,10 @@