Доделали все в коде, остается лишь подключить базу данных

This commit is contained in:
Tonby73 2024-11-24 10:30:27 +04:00
parent 617b14ec3f
commit ea59725800
2 changed files with 45 additions and 12 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ProjectTourAgency.Enities;
using ProjectTourAgency.Repositories;
@ -31,7 +32,20 @@ public class TourRepository : ITourRepository
using var connection = new SqlConnection(_connectionString.ConnectionString);
connection.Open();
using var transaction = connection.BeginTransaction();
var queryInsert;
var queryInsert = @"
INSERT INTO Tour(EmployeeId, RouteId, DepartureDate, ClientTours)
VALUES (@EmployeeId, @RouteId, @DepartureDate, @ClientTours)
SELECT MAX(Id) FROM Tour
";
var tourID = connection.QueryFirst<int>(queryInsert, tour, transaction);
var querySubInsert = @"
INSERT INTO ClientTour(ClientId, TourId, Cost)
VALUES (@ClientId, @TourId, @Cost)";
foreach (var elem in tour.ClientTours)
{
connection.Execute(querySubInsert, new { tourID, elem.ClientId, elem.Cost });
}
transaction.Commit();
}
catch (Exception ex)
{
@ -42,19 +56,40 @@ public class TourRepository : ITourRepository
public void DeleteTour(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объекьт: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
DELETE FROM Tour
WHERE Id = @id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Tour ReadTourById(int id)
{
return Tour.CreateEntity(0,0, 0,DateTime.Now, []);
}
public IEnumerable<Tour> ReadTours()
{
return [];
}
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM Tour";
var tour = connection.Query<Tour>(querySelect);
_logger.LogDebug("Получение объектов {json}", JsonConvert.SerializeObject(tour));
return tour;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении");
throw;
}
public void UpdateTour(Tour tour)
{
}
}

View File

@ -11,11 +11,9 @@ public interface ITourRepository
{
IEnumerable<Tour> ReadTours();
Tour ReadTourById(int id);
void CreateTour(Tour tour);
void UpdateTour(Tour tour);
void DeleteTour(int id);
}