78 lines
3.5 KiB
C#
78 lines
3.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using uWine.Core.Domain.Entities;
|
|
using uWine.Core.Domain.Repositories;
|
|
|
|
namespace uWine.Core.Infrastructure.Repository
|
|
{
|
|
public class MariaDbAddressRepository : MariaDbServerRepository, IAddressRepository
|
|
{
|
|
public MariaDbAddressRepository(IConfiguration configuration) : base(configuration)
|
|
{
|
|
}
|
|
|
|
public void Insert(Address address)
|
|
{
|
|
using (var connection = Connection())
|
|
{
|
|
connection.Open();
|
|
var command = connection.CreateCommand();
|
|
command.CommandText = $@"INSERT INTO t_addresses (
|
|
country,
|
|
postal_code,
|
|
county,
|
|
province,
|
|
city,
|
|
street,
|
|
house_number,
|
|
latitude,
|
|
longitude,
|
|
active
|
|
) VALUES (
|
|
@country,
|
|
@postal_code,
|
|
@county,
|
|
@province,
|
|
@city,
|
|
@street,
|
|
@house_number,
|
|
@latitude,
|
|
@longitude,
|
|
@active
|
|
)";
|
|
command.Parameters.AddWithValue("@country", address.Country);
|
|
command.Parameters.AddWithValue("@postal_code", address.PostalCode);
|
|
command.Parameters.AddWithValue("@county", address.County);
|
|
command.Parameters.AddWithValue("@province", address.Province);
|
|
command.Parameters.AddWithValue("@city", address.City);
|
|
command.Parameters.AddWithValue("@street", address.Street);
|
|
command.Parameters.AddWithValue("@house_number", address.HouseNumber);
|
|
command.Parameters.AddWithValue("@latitude", address.Latitude);
|
|
command.Parameters.AddWithValue("@longitude", address.Longitude);
|
|
command.Parameters.AddWithValue("@active", address.Active ? 1 : 0);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
public List<Address> OwnerAddresses(int OwnerId)
|
|
{
|
|
var addresses = new List<Address>();
|
|
using (var connection = Connection())
|
|
{
|
|
connection.Open();
|
|
var command = connection.CreateCommand();
|
|
command.CommandText = $@"SELECT a.* FROM t_addresses a
|
|
JOIN t_owners_addresses oa ON a.id = oa.address_id
|
|
WHERE oa.owner_id = @OwnerId";
|
|
command.Parameters.AddWithValue("@OwnerId", OwnerId);
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
addresses.Add(Address.FromRecord(reader));
|
|
}
|
|
}
|
|
}
|
|
return addresses;
|
|
}
|
|
}
|
|
} |