Skip to content

Commit

Permalink
Merge pull request #5 from ammarGamal123/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ammarGamal123 authored Dec 30, 2024
2 parents b260656 + 66e4814 commit 59bd72f
Show file tree
Hide file tree
Showing 20 changed files with 2,811 additions and 139 deletions.
2 changes: 1 addition & 1 deletion ProtoSCADA.Api/Controllers/MachineController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static MachineDto MapToDto(Machine machine)
MachineID = machine.ID,
MachineType = machine.Type,
Status = machine.Status,
LastMaintance = machine.LastMaintance,
LastMaintance = machine.LastMaintenance,
FactorName = machine.Factory?.Name // Safely access Factory properties
};
}
Expand Down
4 changes: 2 additions & 2 deletions ProtoSCADA.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<ActionResult<ProcessResult<IEnumerable<UserDto>>>> GetAllUsers
ID = user.ID,
Name = user.Name,
Email = user.Email,
Role = user.Role,
Role = user.Role.RoleName,
CreatedAt = user.CreatedAt
});

Expand Down Expand Up @@ -72,7 +72,7 @@ public async Task<ActionResult<UserDto>> GetUserByID(int id)
ID = result.Data.ID,
Name = result.Data.Name,
Email = result.Data.Email,
Role = result.Data.Role,
Role = result.Data.Role.RoleName,
CreatedAt = result.Data.CreatedAt
};
return Ok(ProcessResult<UserDto>.Success(result.Message, userDto));
Expand Down
173 changes: 172 additions & 1 deletion ProtoSCADA.DataService/Context/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : ba

public DbSet<Alert> Alerts { get; set; }
public DbSet<Event> Events { get; set; }
public DbSet<Factory> Factorys { get; set; }
public DbSet<Factory> Factories { get; set; }
public DbSet<Machine> Machines { get; set; }
public DbSet<Metric> Metrics { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Line> Lines { get; set; }
public DbSet<Report> Reports { get; set; }
public DbSet<Role> Roles { get; set; }


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Expand All @@ -34,6 +37,174 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
}


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// User and Role
modelBuilder.Entity<User>()
.HasOne(u => u.Role)
.WithMany()
.HasForeignKey(u => u.RoleID)
.OnDelete(DeleteBehavior.Restrict);

// User and Factory
modelBuilder.Entity<User>()
.HasOne(u => u.Factory)
.WithMany(f => f.Users)
.HasForeignKey(u => u.FactoryID)
.OnDelete(DeleteBehavior.Restrict);

// User and Line
modelBuilder.Entity<User>()
.HasOne(u => u.Line)
.WithMany()
.HasForeignKey(u => u.LineID)
.OnDelete(DeleteBehavior.Restrict);

// Factory and Machines
modelBuilder.Entity<Machine>()
.HasOne(m => m.Factory)
.WithMany(f => f.Machines)
.HasForeignKey(m => m.FactoryID)
.OnDelete(DeleteBehavior.Restrict);

// Factory and Lines
modelBuilder.Entity<Line>()
.HasOne(l => l.Factory)
.WithMany(f => f.Lines)
.HasForeignKey(l => l.FactoryID)
.OnDelete(DeleteBehavior.Restrict);

// Factory and Reports
modelBuilder.Entity<Report>()
.HasOne(r => r.Factory)
.WithMany(f => f.Reports)
.HasForeignKey(r => r.FactoryID)
.OnDelete(DeleteBehavior.Restrict);

// Factory and Alerts
modelBuilder.Entity<Alert>()
.HasOne(a => a.Factory)
.WithMany(f => f.Alerts)
.HasForeignKey(a => a.FactoryID)
.OnDelete(DeleteBehavior.Restrict);

// Factory and Events
modelBuilder.Entity<Event>()
.HasOne(e => e.Factory)
.WithMany(f => f.Events)
.HasForeignKey(e => e.FactoryID)
.OnDelete(DeleteBehavior.Restrict);

// Line and Machines
modelBuilder.Entity<Machine>()
.HasOne(m => m.Line)
.WithMany(l => l.Machines)
.HasForeignKey(m => m.LineID)
.OnDelete(DeleteBehavior.Restrict);

// Line and Supervisor (User)
modelBuilder.Entity<Line>()
.HasOne(l => l.Supervisor)
.WithMany()
.HasForeignKey(l => l.SupervisorID)
.OnDelete(DeleteBehavior.Restrict);

// Line and Alerts
modelBuilder.Entity<Alert>()
.HasOne(a => a.Line)
.WithMany(l => l.Alerts)
.HasForeignKey(a => a.LineID)
.OnDelete(DeleteBehavior.Restrict);

// Line and Events
modelBuilder.Entity<Event>()
.HasOne(e => e.Line)
.WithMany(l => l.Events)
.HasForeignKey(e => e.LineID)
.OnDelete(DeleteBehavior.Restrict);

// Line and Reports
modelBuilder.Entity<Report>()
.HasOne(r => r.Line)
.WithMany(l => l.Reports)
.HasForeignKey(r => r.LineID)
.OnDelete(DeleteBehavior.Restrict);

// Machine and Metrics
modelBuilder.Entity<Metric>()
.HasOne(m => m.Machine)
.WithMany(ma => ma.Metrics)
.HasForeignKey(m => m.MachineID)
.OnDelete(DeleteBehavior.Restrict);

// Machine and Alerts
modelBuilder.Entity<Alert>()
.HasOne(a => a.Machine)
.WithMany(m => m.Alerts)
.HasForeignKey(a => a.MachineID)
.OnDelete(DeleteBehavior.Restrict);

// Machine and Events
modelBuilder.Entity<Event>()
.HasOne(e => e.Machine)
.WithMany(m => m.Events)
.HasForeignKey(e => e.MachineID)
.OnDelete(DeleteBehavior.Restrict);

// Report and User (CreatedByUser)
modelBuilder.Entity<Report>()
.HasOne(r => r.CreatedByUser)
.WithMany()
.HasForeignKey(r => r.CreatedByUserID)
.OnDelete(DeleteBehavior.Restrict);

// Event and User
modelBuilder.Entity<Event>()
.HasOne(e => e.User)
.WithMany()
.HasForeignKey(e => e.UserID)
.OnDelete(DeleteBehavior.Restrict);

// Enum Conversions
modelBuilder.Entity<Machine>()
.Property(m => m.Status)
.HasConversion<byte>();

modelBuilder.Entity<Alert>()
.Property(a => a.Condition)
.HasConversion<byte>();

// Default Values for CreatedAt
modelBuilder.Entity<Alert>()
.Property(a => a.CreatedAt)
.HasDefaultValueSql("GETDATE()");

modelBuilder.Entity<Event>()
.Property(e => e.CreatedAt)
.HasDefaultValueSql("GETDATE()");


// Indexes
modelBuilder.Entity<Machine>()
.HasIndex(m => m.FactoryID)
.HasDatabaseName("IX_Machine_FactoryID");

modelBuilder.Entity<Line>()
.HasIndex(l => l.FactoryID)
.HasDatabaseName("IX_Line_FactoryID");

modelBuilder.Entity<Report>()
.HasIndex(r => r.FactoryID)
.HasDatabaseName("IX_Report_FactoryID");

modelBuilder.Entity<Alert>()
.HasIndex(a => a.FactoryID)
.HasDatabaseName("IX_Alert_FactoryID");

modelBuilder.Entity<Event>()
.HasIndex(e => e.FactoryID)
.HasDatabaseName("IX_Event_FactoryID");
}

}
}
Loading

0 comments on commit 59bd72f

Please sign in to comment.