sql database rework

This commit is contained in:
ismail 2018-06-22 16:29:27 +02:00
parent 41418b2495
commit c46a53682b
11 changed files with 243 additions and 40 deletions

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using EHEC_Server.DataBuilder;
using System.Data.Entity;
namespace EHEC_Server.DataBuilder
{
public class DataSeeder : EHEC_DBEntities
{
//protected override void Seed(EHEC_DBEntities context)
// {
//var doctors = GenerateDataBuilder.CreateDoctors();
// foreach (var d in doctors)
// {
// // context.Doctor.Add(d);
// }
// }
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EHEC_Server.DataBuilder
{
public class GenerateDataBuilder
{
private static List<Doctor> Doctors = new List<Doctor>();
private static List<Patient> Patients = new List<Patient>();
private static List<Origin> Origins = new List<Origin>();
private static List<Result> Results = new List<Result>();
private static List<Exam> Exams = new List<Exam>();
private static Random Rnd = new Random();
private static List<string> FirstNames = new List<string>(new string[]
{
"Michael", "Andreas", "Stefan", "Ivan","Adrien"
});
private static List<string> LastNames = new List<string>(new string[]
{
"Meister", "Schär", "Eberhard", "Zingg", "Howald", "Aebi", "Feldmann"
});
private static List<string> DoctorOrigins = new List<string>(new string[]
{
"SanktMonika", "DisneyWorld", "Hell", "Heaven"
});
private static List<string> Street = new List<string>(new string[]
{
"Rosenauweg", "Schessstrasse", "Hardstrasse", "Gehweg", "Bananastreet", "Moosweg", "Feldstrasse"
});
private static List<string> BirthDate = new List<string>(new string[]
{
"12991212", "20000112",
});
private static List<string> City = new List<string>(new string[]
{
"Meister", "Schär", "Eberhard", "Zingg", "Howald", "Aebi", "Feldmann"
});
public static List<Patient> CreatePatients()
{
//int Counter = Patients.Count();
//for (int i = 0; i < Counter; i++)
foreach (var c in FirstNames)
{
for (int j = 0; j < 10; j++)
{
Patient patient = new Patient(
FirstNames[Rnd.Next(1, FirstNames.Count())],
LastNames[Rnd.Next(1, LastNames.Count())],
BirthDate[Rnd.Next(1, BirthDate.Count())],
Street[Rnd.Next(1, Street.Count())],
City[Rnd.Next(1, City.Count())]);
Patients.Add(patient);
}
}
return Patients;
}
public static List<Doctor> CreateDoctors()
{
//int Counter = Patients.Count();
//for (int i = 0; i < Counter; i++)
foreach (var c in FirstNames)
{
for (int j = 0; j < 10; j++)
{
Doctor doctor = new Doctor(
FirstNames[Rnd.Next(1, FirstNames.Count())],
LastNames[Rnd.Next(1, LastNames.Count())],
DoctorOrigins[Rnd.Next(1, DoctorOrigins.Count())]);
Doctors.Add(doctor);
}
}
return Doctors;
}
}
}

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
namespace EHEC_Server.DatabaseAccess
{
public class DoctorAccess
{
public bool CreateDoctor(Doctor doctor)
{
try
{
using (EHEC_DBEntities ctx = new EHEC_DBEntities())
{
ctx.Doctor.Add(doctor);
ctx.SaveChanges();
}
return true;
}
catch (Exception)
{
return false;
}
}
public List<Doctor> GetAllDoctors()
{
using (EHEC_DBEntities ctx = new EHEC_DBEntities())
{
return ctx.Doctor.ToList();
}
}
}
}

View File

@ -9,17 +9,41 @@
namespace EHEC_Server
{
using EHEC_Server.DataBuilder;
using System;
using System.Collections.Generic;
using System.Linq;
using EHEC_Server.DatabaseAccess;
public partial class Doctor
public partial class Doctor : DoctorAccess
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Doctor()
{
this.Exam = new HashSet<Exam>();
}
public void CreateDoctors(Doctor doctor)
{
using (EHEC_DBEntities ctx = new EHEC_DBEntities())
{
var doctors = GenerateDataBuilder.CreateDoctors();
foreach (var d in doctors)
{
ctx.Doctor.Add(d);
}
}
}
public Doctor(string firstname, string lastname, string doctororigin)
{
this.FirstName = firstname;
this.LastName = lastname;
this.DoctorOrigin = doctororigin;
}
public int DoctorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
@ -29,28 +53,6 @@ namespace EHEC_Server
public virtual ICollection<Exam> Exam { get; set; }
public bool CreateDoctor(Doctor doctor)
{
try
{
using (EHEC_DBEntities ctx = new EHEC_DBEntities())
{
ctx.Doctor.Add(doctor);
ctx.SaveChanges();
}
return true;
}
catch (Exception)
{
return false;
}
}
public List<Doctor> GetAllDoctors()
{
using (EHEC_DBEntities ctx = new EHEC_DBEntities())
{
return ctx.Doctor.ToList();
}
}
}
}

View File

@ -106,6 +106,9 @@
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="DatabaseAccess\DoctorAccess.cs" />
<Compile Include="DataBuilder\DataSeeder.cs" />
<Compile Include="DataBuilder\GenerateDataBuilder.cs" />
<Compile Include="Doctor.cs">
<DependentUpon>Model.tt</DependentUpon>
</Compile>
@ -145,6 +148,7 @@
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@ -11,6 +11,9 @@ namespace EHEC_Server
[ServiceContract]
public interface IService
{
[OperationContract]
void CreateRandomData(Doctor doctor);
[OperationContract]
void WriteDoctor(Doctor doctor);

View File

@ -1,4 +1,4 @@
// T4 code generation is enabled for model 'C:\Users\ism_c\source\repos\EHEC_Server\EHEC_Server\Model.edmx'.
// T4 code generation is enabled for model 'C:\Users\ism_c\source\repos\oop_II-6\EHEC_Server\EHEC_Server\Model.edmx'.
// To enable legacy code generation, change the value of the 'Code Generation Strategy' designer
// property to 'Legacy ObjectContext'. This property is available in the Properties Window when the model
// is open in the designer.

View File

@ -175,7 +175,7 @@
<Property Name="PatientId" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="FirstName" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="LastName" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="BirthDate" Type="DateTime" Nullable="false" Precision="3" />
<Property Name="BirthDate" Type="String" Nullable="false" />
<Property Name="Street" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="City" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" Nullable="false" />
<NavigationProperty Name="Exam" Relationship="Self.FK_PatientExam" FromRole="Patient" ToRole="Exam" />

View File

@ -21,10 +21,20 @@ namespace EHEC_Server
this.Exam = new HashSet<Exam>();
}
public Patient(string firstname, string lastname, string birthDate, string street, string city)
{
this.FirstName = firstname;
this.LastName = lastname;
this.BirthDate = birthDate;
this.Street = street;
this.City = city;
}
public int PatientId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public System.DateTime BirthDate { get; set; }
//public System.DateTime BirthDate { get; set; }
public string BirthDate { get; set; }
public string Street { get; set; }
public string City { get; set; }

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using EHEC_Server.DataBuilder;
using EHEC_Server.DatabaseAccess;
namespace EHEC_Server
{
@ -11,7 +13,12 @@ namespace EHEC_Server
// NOTE: In order to launch WCF Test Client for testing this service, please select Service.svc or Service.svc.cs at the Solution Explorer and start debugging.
public class Service : IService
{
public List<Doctor> GetDoctors()
public void CreateRandomData(Doctor doctor)
{
Doctor dataaccess = new Doctor();
dataaccess.CreateDoctors(doctor);
}
public List<Doctor> GetDoctors()
{
Doctor dataaccess = new Doctor();
return dataaccess.GetAllDoctors();

View File

@ -89,7 +89,8 @@ CREATE TABLE [dbo].[Patient] (
[LastName] nvarchar(max) NOT NULL,
[BirthDate] datetime NOT NULL,
[Street] nvarchar(max) NOT NULL,
[City] nvarchar(max) NOT NULL
[City] nvarchar(max) NOT NULL,
[Region] nvarchar(max) NOT NULL
);
GO
@ -98,15 +99,16 @@ CREATE TABLE [dbo].[Doctor] (
[DoctorId] int IDENTITY(1,1) NOT NULL,
[FirstName] nvarchar(max) NOT NULL,
[LastName] nvarchar(max) NOT NULL,
[DoctorOrigin] nvarchar(max) NOT NULL
[DoctorOrigin] nvarchar(max) NOT NULL,
[Region] nvarchar(max) NOT NULL
);
GO
-- Creating table 'Origin'
CREATE TABLE [dbo].[Origin] (
[OriginId] int IDENTITY(1,1) NOT NULL,
[SicknessId] int NOT NULL,
[Name] nvarchar(max) NOT NULL,
[Food] nvarchar(max) NOT NULL,
[Street] nvarchar(max) NOT NULL,
[City] nvarchar(max) NOT NULL
);
@ -117,8 +119,6 @@ CREATE TABLE [dbo].[Exam] (
[ExamId] int IDENTITY(1,1) NOT NULL,
[DoctorId] int NOT NULL,
[PatientId] int NOT NULL,
[SicknessStatus] bit NOT NULL,
[SicknessDesignation] nvarchar(max) NOT NULL,
[OriginOriginId] int NOT NULL,
[Result_ResultId] int NOT NULL
);
@ -128,13 +128,25 @@ GO
CREATE TABLE [dbo].[Result] (
[ResultId] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(max) NOT NULL,
[Description] nvarchar(max) NOT NULL
);
GO
-- Creating table 'Origin_Exam'
CREATE TABLE [dbo].[Origin_Exam] (
[Origin_ExamId] int IDENTITY(1,1) NOT NULL,
[OriginOriginId] int NOT NULL,
[ExamExamId] int NOT NULL
);
GO
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [Origin_ExamId] in table 'Origin_Exam'
ALTER TABLE [dbo].[Origin_Exam]
ADD CONSTRAINT [PK_Origin_Exam]
PRIMARY KEY CLUSTERED ([Origin_ExamId] ASC);
GO
-- Creating primary key on [PatientId] in table 'Patient'
ALTER TABLE [dbo].[Patient]
@ -170,6 +182,9 @@ GO
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [Result_ResultId] in table 'Exam'
ALTER TABLE [dbo].[Exam]
ADD CONSTRAINT [FK_ResultExam]
@ -215,21 +230,37 @@ ON [dbo].[Exam]
([PatientId]);
GO
-- Creating foreign key on [OriginOriginId] in table 'Exam'
ALTER TABLE [dbo].[Exam]
ADD CONSTRAINT [FK_OriginExam]
-- Creating foreign key on [OriginOriginId] in table 'Origin_Exam'
ALTER TABLE [dbo].[Origin_Exam]
ADD CONSTRAINT [FK_Origin]
FOREIGN KEY ([OriginOriginId])
REFERENCES [dbo].[Origin]
([OriginId])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO
-- Creating non-clustered index for FOREIGN KEY 'FK_OriginExam'
CREATE INDEX [IX_FK_OriginExam]
ON [dbo].[Exam]
-- Creating non-clustered index for FOREIGN KEY 'FK_Origin_Exam'
CREATE INDEX [IX_FK_Origin_Exam]
ON [dbo].[Origin_Exam]
([OriginOriginId]);
GO
-- Creating foreign key on [ExamId] in table 'Origin_Exam'
ALTER TABLE [dbo].[Origin_Exam]
ADD CONSTRAINT [FK_Exam]
FOREIGN KEY ([ExamExamId])
REFERENCES [dbo].[Exam]
([ExamId])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO
-- Creating non-clustered index for FOREIGN KEY 'FK_Origin_Exam'
CREATE INDEX [IZ_FK_Origin_Exam]
ON [dbo].[Origin_Exam]
([ExamExamId]);
GO
-- --------------------------------------------------
PRINT 'Erfolgreich erstellt..!';
-- Script has ended