Files
cinema-ticket-management-sy…/data/sql/halls_repository.cpp
T
2026-07-12 14:34:52 +04:00

100 lines
2.9 KiB
C++

#include "halls_repository.h"
#include "create_hall_request.h"
#include "hall_dto.h"
#include "repository_result.h"
#include "update_hall_request.h"
#include <QSqlError>
#include <QSqlQuery>
HallsRepository::HallsRepository(QSqlDatabase &database, QObject *parent)
: BaseSqlTableRepository(database, parent) {}
RepositoryResult HallsRepository::createHall(const CreateHallRequest &request) {
QSqlQuery query = foreignKeysOnQuery();
query.prepare("INSERT INTO halls (hall_name, capacity) VALUES "
"(:hall_name, :capacity)");
query.bindValue(":hall_name", request.hallName());
query.bindValue(":capacity", request.capacity());
if (!query.exec()) {
return RepositoryResult::failureResult(query.lastError().text());
}
emit dataCreated();
return RepositoryResult::successResult();
}
RepositoryResult HallsRepository::getHallById(qint32 id, HallDTO &hall) const {
QSqlQuery query = foreignKeysOnQuery();
query.prepare(
"SELECT hall_id, hall_name, capacity FROM halls WHERE hall_id = :id");
query.bindValue(":id", id);
if (!query.exec()) {
return RepositoryResult::failureResult(query.lastError().text());
}
if (!query.next()) {
return RepositoryResult::failureResult("Hall not found");
}
hall = mapQueryToDTO(query);
emit dataRetrieved();
return RepositoryResult::successResult();
}
RepositoryResult HallsRepository::getAllHalls(QVector<HallDTO> &halls) const {
QSqlQuery query = foreignKeysOnQuery();
query.prepare("SELECT hall_id, hall_name, capacity FROM halls");
if (!query.exec()) {
return RepositoryResult::failureResult(query.lastError().text());
}
while (query.next()) {
halls.append(mapQueryToDTO(query));
}
emit dataRetrieved();
return RepositoryResult::successResult();
}
RepositoryResult HallsRepository::updateHall(const UpdateHallRequest &request) {
QSqlQuery query = foreignKeysOnQuery();
query.prepare("UPDATE halls SET hall_name = :hall_name, capacity = :capacity "
"WHERE hall_id = :hall_id");
query.bindValue(":capacity", request.capacity());
query.bindValue(":hall_id", request.hallId());
query.bindValue(":hall_name", request.hallName());
if (!query.exec()) {
return RepositoryResult::failureResult(query.lastError().text());
}
emit dataUpdated();
return RepositoryResult::successResult();
}
RepositoryResult HallsRepository::deleteHallById(qint32 hallId) {
QSqlQuery query = foreignKeysOnQuery();
query.prepare("DELETE FROM halls WHERE hall_id = :hall_id");
query.bindValue(":hall_id", hallId);
if (!query.exec()) {
return RepositoryResult::failureResult(query.lastError().text());
}
emit dataDeleted();
return RepositoryResult::successResult();
}
HallDTO HallsRepository::mapQueryToDTO(const QSqlQuery &query) const {
HallDTO hallDTO;
hallDTO.setCapacity(query.value("capacity").toInt());
hallDTO.setHallId(query.value("hall_id").toInt());
hallDTO.setHallName(query.value("hall_name").toString());
return hallDTO;
}