Правки.

This commit is contained in:
Андрей Байгулов 2024-05-07 21:19:19 +04:00
parent 51230648ff
commit 57b2a79cc6
18 changed files with 141 additions and 127 deletions

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SUBD.ComponentInterfaces;
using SUBD.ComponentEntity;
namespace SUBD.ComponentDatabase
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component Create(ComponentViewModel model)
{
return new Component
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace SUBD.ComponentDatabase
{
public class ComponentsDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder)
{
if (OptionsBuilder.IsConfigured == false)
{
OptionsBuilder.UseNpgsql(@"Host=localhost;Database=Components;Username=postgres;Password=postgres");
}
base.OnConfiguring(OptionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<Component> Components { set; get; }
}
}

View File

@ -1,56 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SUBD.ComponentEntity
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component Create(ComponentViewModel model)
{
return new Component
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SUBD.ComponentInterfaces;
namespace SUBD.ComponentEntity
{
public class ComponentBindingModel : IComponentModel
public class ComponentBindingModel : IComponentModel
{
public int Id { get; set; }
public string ComponentName { get; set; } = string.Empty;

View File

@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SUBD.ComponentInterfaces;
namespace SUBD.ComponentEntity
{
public class ComponentLogic : IComponentLogic
public class ComponentLogic : IComponentLogic
{
private readonly IComponentStorage _componentStorage;
public ComponentLogic(IComponentStorage componentStorage)

View File

@ -4,10 +4,12 @@ using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SUBD.ComponentDatabase;
using SUBD.ComponentInterfaces;
namespace SUBD.ComponentEntity
{
public class ComponentStorage : IComponentStorage
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
@ -40,7 +42,7 @@ namespace SUBD.ComponentEntity
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
var newComponent = ComponentDatabase.Component.Create(model);
if (newComponent == null)
{
return null;

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SUBD.ComponentInterfaces;
namespace SUBD.ComponentEntity
{

View File

@ -1,28 +0,0 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace SUBD.ComponentEntity
{
public class ComponentsDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder)
{
if (OptionsBuilder.IsConfigured == false)
{
OptionsBuilder.UseNpgsql(@"Host=localhost;Database=Components;Username=postgres;Password=postgres");
}
base.OnConfiguring(OptionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<Component> Components { set; get; }
}
}

View File

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace SUBD.ComponentEntity
{
public interface IComponentModel
{
int Id { get; }
string ComponentName { get; }
double Cost { get; }
}
}

View File

@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SUBD.ComponentEntity
{
public interface IComponentStorage
{
List<ComponentViewModel> GetFullList();
List<ComponentViewModel> GetFilteredList(ComponentSearchModel model);
ComponentViewModel? GetElement(ComponentSearchModel model);
ComponentViewModel? Insert(ComponentBindingModel model);
ComponentViewModel? Update(ComponentBindingModel model);
ComponentViewModel? Delete(ComponentBindingModel model);
}
}

View File

@ -3,8 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SUBD.ComponentEntity;
namespace SUBD.ComponentEntity
namespace SUBD.ComponentInterfaces
{
public interface IComponentLogic
{

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace SUBD.ComponentInterfaces
{
public interface IComponentModel
{
int Id { get; }
string ComponentName { get; }
double Cost { get; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SUBD.ComponentEntity;
namespace SUBD.ComponentInterfaces
{
public interface IComponentStorage
{
List<ComponentViewModel> GetFullList();
List<ComponentViewModel> GetFilteredList(ComponentSearchModel model);
ComponentViewModel? GetElement(ComponentSearchModel model);
ComponentViewModel? Insert(ComponentBindingModel model);
ComponentViewModel? Update(ComponentBindingModel model);
ComponentViewModel? Delete(ComponentBindingModel model);
}
}

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SUBD.ComponentEntity;
using SUBD.ComponentInterfaces;
namespace SUBD
{

View File

@ -1,4 +1,5 @@
using SUBD.ComponentEntity;
using SUBD.ComponentInterfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -11,7 +12,7 @@ using System.Windows.Forms;
namespace SUBD
{
public partial class FormComponents : Form
public partial class FormComponents : Form
{
private readonly IComponentLogic _logic;
public FormComponents(IComponentLogic logic)

View File

@ -4,7 +4,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SUBD.ComponentEntity;
using SUBD.ComponentDatabase;
#nullable disable

View File

@ -3,7 +3,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SUBD.ComponentEntity;
using SUBD.ComponentDatabase;
#nullable disable

View File

@ -1,10 +1,11 @@
using SUBD.ComponentEntity;
using Microsoft.Extensions.DependencyInjection;
using System;
using SUBD.ComponentInterfaces;
namespace SUBD
{
internal static class Program
internal static class Program
{
private static ServiceProvider? _serviceProvider;
public static ServiceProvider? ServiceProvider => _serviceProvider;