2024-05-02 20:30:14 +04:00
|
|
|
|
using BeautySalonDBModels.Models;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BeautySalonDBModels.Implements
|
|
|
|
|
{
|
2024-05-07 22:33:37 +04:00
|
|
|
|
public class ServiceDatabase : AbstractWorkWithStorage<Service>
|
2024-05-02 20:30:14 +04:00
|
|
|
|
{
|
|
|
|
|
public override void Add(Service service)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:59:08 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("INSERT INTO services (service_id, service_name, price, specialisation_id) " +
|
|
|
|
|
"VALUES ((nextval('seq_service')), @ServiceName, @Price, @SpecialisationId)", conn);
|
2024-05-02 20:30:14 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@ServiceName", service.ServiceName);
|
|
|
|
|
cmd.Parameters.AddWithValue("@Price", service.Price);
|
|
|
|
|
cmd.Parameters.AddWithValue("@SpecialisationId", service.SpecialisationId);
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Service? GetObject(int id)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:59:08 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM services WHERE service_id = @id", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@id", id);
|
2024-05-02 20:30:14 +04:00
|
|
|
|
using var reader = cmd.ExecuteReader();
|
|
|
|
|
if (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
return new Service
|
|
|
|
|
{
|
|
|
|
|
ServiceId = reader.GetInt32(0),
|
|
|
|
|
ServiceName = reader.GetString(1),
|
|
|
|
|
Price = reader.GetDouble(2),
|
|
|
|
|
SpecialisationId = reader.GetInt32(3)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override List<Service> GetObjects()
|
|
|
|
|
{
|
|
|
|
|
var services = new List<Service>();
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM services order by service_id", conn);
|
|
|
|
|
using var reader = cmd.ExecuteReader();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
services.Add(new Service
|
|
|
|
|
{
|
|
|
|
|
ServiceId = reader.GetInt32(0),
|
|
|
|
|
ServiceName = reader.GetString(1),
|
|
|
|
|
Price = reader.GetDouble(2),
|
|
|
|
|
SpecialisationId = reader.GetInt32(3)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Service? Remove(int id)
|
|
|
|
|
{
|
|
|
|
|
var service = GetObject(id);
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:59:08 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("delete from services where service_id = @id", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@id", id);
|
2024-05-02 20:30:14 +04:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
return service;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update(Service service)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:59:08 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("update service set service_name = @name, " +
|
|
|
|
|
"price = @price, specialisation_id = @SpecialisationId where service_id = @id", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@name", service.ServiceName);
|
|
|
|
|
cmd.Parameters.AddWithValue("@price", service.Price);
|
|
|
|
|
cmd.Parameters.AddWithValue("@SpecialisationId", service.SpecialisationId);
|
|
|
|
|
cmd.Parameters.AddWithValue("@id", service.ServiceId);
|
2024-05-02 20:30:14 +04:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
2024-05-08 03:05:15 +04:00
|
|
|
|
|
|
|
|
|
public string GetNameSpecialisation(Service service)
|
|
|
|
|
{
|
|
|
|
|
SpecialisationDatabase db = new SpecialisationDatabase();
|
|
|
|
|
int specId = service.SpecialisationId;
|
|
|
|
|
List<Specialisation> specialisations = db.GetObjects();
|
|
|
|
|
|
|
|
|
|
foreach (Specialisation specialisation in specialisations)
|
|
|
|
|
{
|
|
|
|
|
if (specialisation.SpecialisationId == specId) return specialisation.Name.ToString();
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2024-05-08 12:36:17 +04:00
|
|
|
|
|
|
|
|
|
public List<Master> GetFilteredListByService(int serviceId)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<Master>();
|
|
|
|
|
var service = GetObject(serviceId);
|
|
|
|
|
if(service != null)
|
|
|
|
|
{
|
|
|
|
|
int specId = service.SpecialisationId;
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = new NpgsqlCommand("select * from masters where specialisation_id = @specId", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@specId", specId);
|
|
|
|
|
using var reader = cmd.ExecuteReader();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
list.Add(new Master
|
|
|
|
|
{
|
|
|
|
|
MasterId = reader.GetInt32(0),
|
|
|
|
|
SpecialisationId = reader.GetInt32(1),
|
|
|
|
|
FIO = reader.GetString(2)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2024-05-02 20:30:14 +04:00
|
|
|
|
}
|
|
|
|
|
}
|