добавила сеты и почти реализовала хранилище клиента

This commit is contained in:
Елена Бакальская 2024-05-02 12:54:17 +04:00
parent f24716aee0
commit e2035800e3
8 changed files with 101 additions and 26 deletions

View File

@ -1,13 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BeautySalonDBModels.Models;
using Npgsql;
namespace BeautySalonDBModels
{
public abstract class AbstractWorkWithStorage
public abstract class AbstractWorkWithStorage<T>
{
public abstract void AddClient(Clien);
public abstract void Add(T obj);
public abstract T? Remove(int id);
public abstract void Update(T obj);
public abstract List<T> GetObjects();
public abstract T? GetObject(int id);
public NpgsqlConnection GetConnection()
{
return new NpgsqlConnection("Host=localhost;Port=5555;Username=elina;Database=beauty_salon;Password=elina");
}
}
}

View File

@ -0,0 +1,69 @@
using BeautySalonDBModels.Models;
using Npgsql;
namespace BeautySalonDBModels.Implements
{
public class ClientDB : AbstractWorkWithStorage<Client>
{
public override void Add(Client client)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("INSERT INTO clients (fio, age) VALUES (@FIO, @Age)", conn);
cmd.Parameters.AddWithValue("@FIO", client.FIO);
cmd.Parameters.AddWithValue("@Age", client.Age);
cmd.ExecuteNonQuery();
}
public override Client? GetObject(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM clients WHERE client_id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Client
{
ClientId = reader.GetInt32(0),
FIO = reader.GetString(1),
Age = reader.GetInt32(2)
};
}
return null;
}
public override List<Client> GetObjects()
{
var clients = new List<Client>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM clients order by id", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
clients.Add(new Client
{
ClientId = reader.GetInt32(0),
FIO = reader.GetString(1),
Age = reader.GetInt32(2)
});
}
return clients;
}
public override Client? Remove(int id)
{
var client = GetObject(id);
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("delete from clients where client_id = {id}", conn);
return client;
}
public override void Update(Client client)
{
}
}
}

View File

@ -2,8 +2,8 @@
{
public class Cheque
{
public int ChequeId { get; }
public int ClientId { get; }
public int ReceptionId { get; }
public int ChequeId { get; set; }
public int ClientId { get; set; }
public int ReceptionId { get; set; }
}
}

View File

@ -2,8 +2,8 @@
{
public class Client
{
public int ClientId { get; }
public string FIO { get; } = string.Empty;
public int Age { get; }
public int ClientId { get; set; }
public string FIO { get; set; } = string.Empty;
public int Age { get; set; }
}
}

View File

@ -2,8 +2,8 @@
{
public class Master
{
public int MasterId { get; }
public int SpecialisationId { get; }
public string FIO { get; } = string.Empty;
public int MasterId { get; set; }
public int SpecialisationId { get; set; }
public string FIO { get; set; } = string.Empty;
}
}

View File

@ -2,9 +2,9 @@
{
public class Reception
{
public int ReceptionId { get; }
public int MasterId { get; }
public int ServiceId { get; }
public DateTime DateReception { get; }
public int ReceptionId { get; set; }
public int MasterId { get; set; }
public int ServiceId { get; set; }
public DateTime DateReception { get; set; }
}
}

View File

@ -2,9 +2,9 @@
{
public class Service
{
public int ServiceId { get; }
public string ServiceName { get; } = string.Empty;
public double Price { get; }
public int SpecialisationId { get; }
public int ServiceId { get; set; }
public string ServiceName { get; set; } = string.Empty;
public double Price { get; set; }
public int SpecialisationId { get; set; }
}
}

View File

@ -2,7 +2,7 @@
{
public class Specialisation
{
public int SpecialisationId { get; }
public string Name { get; } = string.Empty;
public int SpecialisationId { get; set; }
public string Name { get; set; } = string.Empty;
}
}