From 07533dbdcb354c7f0bb32aba94be05d5dfddc18f Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 2 Dec 2024 10:23:16 +0100 Subject: [PATCH] Add repositories --- src/repositories/booking.repository.ts | 68 +++++++++++++++++++ src/repositories/client.repository.ts | 93 ++++++++++++++++++++++++++ src/repositories/hotel.repository.ts | 87 ++++++++++++++++++++++++ 3 files changed, 248 insertions(+) diff --git a/src/repositories/booking.repository.ts b/src/repositories/booking.repository.ts index e69de29..943def0 100644 --- a/src/repositories/booking.repository.ts +++ b/src/repositories/booking.repository.ts @@ -0,0 +1,68 @@ +import { Op } from "sequelize"; +import Booking from "../models/booking.model"; + +interface IBookingRepository { + save(booking: Booking): Promise; + retrieveAll(searchParams: {name: string, address: string}): Promise; + retrieveById(bookingId: number): Promise; + update(booking: Booking): Promise; + delete(bookingId: number): Promise; + deleteAll(): Promise; +} + +class BookingRepository implements IBookingRepository { + async save(booking: Booking): Promise { + try { + return await Booking.create({ + hotelId: booking.hotel.id, + clientId: booking.client.id, + }); + } catch (err) { + throw new Error("Failed to create Booking!"); + } + } + + async retrieveAll(): Promise { + try { + return await Booking.findAll(); + } catch (error) { + throw new Error("Failed to retrieve Bookings!"); + } + } + + async retrieveById(bookingId: number): Promise { + try { + return await Booking.findByPk(bookingId); + } catch (error) { + throw new Error("Failed to retrieve Booking!"); + } + } + + async update(booking: Booking): Promise { + throw new Error("Failed to update Booking!"); + } + + async delete(bookingId: number): Promise { + try { + const affectedRows = await Booking.destroy({ where: { id: bookingId } }); + + return affectedRows; + } catch (error) { + throw new Error("Failed to delete Booking!"); + } + } + + async deleteAll(): Promise { + try { + return Booking.destroy({ + where: {}, + truncate: false + }); + } catch (error) { + throw new Error("Failed to delete Bookings!"); + } + } +} + +export default new BookingRepository(); + diff --git a/src/repositories/client.repository.ts b/src/repositories/client.repository.ts index e69de29..2fa3bd5 100644 --- a/src/repositories/client.repository.ts +++ b/src/repositories/client.repository.ts @@ -0,0 +1,93 @@ +import { Op } from "sequelize"; +import Client from "../models/client.model"; + +interface IClientRepository { + save(client: Client): Promise; + retrieveAll(searchParams: {name: string, address: string}): Promise; + retrieveById(clientId: number): Promise; + update(client: Client): Promise; + delete(clientId: number): Promise; + deleteAll(): Promise; +} + +class ClientRepository implements IClientRepository { + async save(client: Client): Promise { + try { + return await Client.create({ + name: client.name, + address: client.address, + phone: client.phone, + }); + } catch (err) { + throw new Error("Failed to create Client!"); + } + } + + async retrieveAll(searchParams: {name?: string, address?: string, phone?: string): Promise { + try { + let condition: SearchCondition = {}; + + if (searchParams?.name) { + condition.name = { [Op.like]: `%${searchParams.name}%` }; + } + + if (searchParams?.address) { + condition.address = { [Op.like]: `%${searchParams.address}%` }; + } + + if (searchParams?.phone) { + condition.phone = { [Op.like]: `%${searchParams.phone}%` }; + } + + return await Client.findAll({ where: condition }); + } catch (error) { + throw new Error("Failed to retrieve Clients!"); + } + } + + async retrieveById(clientId: number): Promise { + try { + return await Client.findByPk(clientId); + } catch (error) { + throw new Error("Failed to retrieve Client!"); + } + } + + async update(client: Client): Promise { + const { id, name, address, phone } = client; + + try { + const affectedRows = await Client.update( + { name, address, phone}, + { where: { id: id } } + ); + + return affectedRows[0]; + } catch (error) { + throw new Error("Failed to update Client!"); + } + } + + async delete(clientId: number): Promise { + try { + const affectedRows = await Client.destroy({ where: { id: clientId } }); + + return affectedRows; + } catch (error) { + throw new Error("Failed to delete Client!"); + } + } + + async deleteAll(): Promise { + try { + return Client.destroy({ + where: {}, + truncate: false + }); + } catch (error) { + throw new Error("Failed to delete Clients!"); + } + } +} + +export default new ClientRepository(); diff --git a/src/repositories/hotel.repository.ts b/src/repositories/hotel.repository.ts index e69de29..d2f857f 100644 --- a/src/repositories/hotel.repository.ts +++ b/src/repositories/hotel.repository.ts @@ -0,0 +1,87 @@ +import { Op } from "sequelize"; +import Hotel from "../models/hotel.model"; + +interface IHotelRepository { + save(hotel: Hotel): Promise; + retrieveAll(searchParams: {name: string, address: string}): Promise; + retrieveById(hotelId: number): Promise; + update(hotel: Hotel): Promise; + delete(hotelId: number): Promise; + deleteAll(): Promise; +} + +class HotelRepository implements IHotelRepository { + async save(hotel: Hotel): Promise { + try { + return await Hotel.create({ + name: hotel.name, + address: hotel.address, + }); + } catch (err) { + throw new Error("Failed to create Hotel!"); + } + } + + async retrieveAll(searchParams: {name?: string, address?: string): Promise { + try { + let condition: SearchCondition = {}; + + if (searchParams?.name) { + condition.name = { [Op.like]: `%${searchParams.name}%` }; + } + + if (searchParams?.address) { + condition.address = { [Op.like]: `%${searchParams.address}%` }; + } + return await Hotel.findAll({ where: condition }); + } catch (error) { + throw new Error("Failed to retrieve Hotels!"); + } + } + + async retrieveById(hotelId: number): Promise { + try { + return await Hotel.findByPk(hotelId); + } catch (error) { + throw new Error("Failed to retrieve Hotel!"); + } + } + + async update(hotel: Hotel): Promise { + const { id, name, address } = hotel; + + try { + const affectedRows = await Hotel.update( + { name, address }, + { where: { id: id } } + ); + + return affectedRows[0]; + } catch (error) { + throw new Error("Failed to update Hotel!"); + } + } + + async delete(hotelId: number): Promise { + try { + const affectedRows = await Hotel.destroy({ where: { id: hotelId } }); + + return affectedRows; + } catch (error) { + throw new Error("Failed to delete Hotel!"); + } + } + + async deleteAll(): Promise { + try { + return Hotel.destroy({ + where: {}, + truncate: false + }); + } catch (error) { + throw new Error("Failed to delete Hotels!"); + } + } +} + +export default new HotelRepository();