добавила реализауию сервиса (услуги)
This commit is contained in:
parent
1498d5b7b7
commit
ca5288a1c4
83
BeautySalon/BeautySalonDBModels/Implements/ServiceDB.cs
Normal file
83
BeautySalon/BeautySalonDBModels/Implements/ServiceDB.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using BeautySalonDBModels.Models;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BeautySalonDBModels.Implements
|
||||
{
|
||||
public class ServiceDB : AbstractWorkWithStorage<Service>
|
||||
{
|
||||
public override void Add(Service service)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("INSERT INTO services (service_name, price, specialisation_id) " +
|
||||
"VALUES (@ServiceName, @Price, @SpecialisationId)", conn);
|
||||
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();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM services WHERE service_id = {id}", conn);
|
||||
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();
|
||||
using var cmd = new NpgsqlCommand("delete from services where service_id = {id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
return service;
|
||||
}
|
||||
|
||||
public override void Update(Service service)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("update service set service_name = {service.ServiceName}, " +
|
||||
"price = {service.Price}, specialisation_id = {service.SpecialisationId} where service_id = {service.ServiceId}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user