import { Op } from "sequelize"; import Hotel from "../models/hotel.model"; interface IHotelRepository { create(hotel: {name: string, address: string}): 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 create(hotel: {name: string, address: string}): 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();