Skip to content

Commit

Permalink
add model: card
Browse files Browse the repository at this point in the history
  • Loading branch information
Byunk committed Jan 13, 2024
1 parent ef60842 commit b704fad
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/controller/card.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Request, Response, NextFunction } from 'express';
import {
CreateCardDto,
DeleteCardDto,
GetCardDto,
UpdateCardDto,
} from '../dto';
import { CardService } from '../service';

class CardController {
private cardService: CardService = new CardService();

findAll = async (req: Request, res: Response, next: NextFunction) => {
const dto = new GetCardDto(
req.params.organization_id,
req.params.year,
req.params.half,
);
const cards = await this.cardService.findAll(dto);
res.json(cards.map((card) => card.toJSON()));
};

create = async (req: Request, res: Response, next: NextFunction) => {
const dto = new CreateCardDto(
req.params.organization_id,
req.params.year,
req.params.half,
req.body.name,
req.body.cardNumber,
);
const card = await this.cardService.create(dto);
res.json(card.toJSON());
};

update = async (req: Request, res: Response, next: NextFunction) => {
const dto = new UpdateCardDto(
req.params.card_id,
req.body.name,
req.body.cardNumber,
);
await this.cardService.update(dto);
res.sendStatus(200);
};

delete = async (req: Request, res: Response, next: NextFunction) => {
const dto = new DeleteCardDto(req.params.card_id);
await this.cardService.delete(dto);
res.sendStatus(200);
};
}

export { CardController };
1 change: 1 addition & 0 deletions src/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './organization.controller';
export * from './user.controller';
export * from './transaction.controller';
export * from './account.controller';
export * from './card.controller';
5 changes: 5 additions & 0 deletions src/db/postgre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
Expense,
Transaction,
Account,
Card,
} = require('../model');

Organization.hasOne(User, {
Expand All @@ -40,6 +41,10 @@ Organization.hasMany(Account, {
});
Account.belongsTo(Organization);

Organization.hasMany(Card, {
onDelete: 'CASCADE',
});

Budget.hasMany(Income, {
onDelete: 'CASCADE',
});
Expand Down
2 changes: 2 additions & 0 deletions src/db/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Account,
AuditPeriod,
Budget,
Card,
Expense,
Income,
Organization,
Expand All @@ -30,6 +31,7 @@ export async function initDB() {
Transaction,
AuditPeriod,
Account,
Card,
];

for (const model of models) {
Expand Down
57 changes: 57 additions & 0 deletions src/dto/card.request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export class GetCardDto {
organizationId: string | number;
year: string;
half: string;

constructor(organizationId: string | number, year: string, half: string) {
this.organizationId = organizationId;
this.year = year;
this.half = half;
}
}

export class CreateCardDto {
organizationId: string | number;
year: string;
half: string;
name: string | undefined;
cardNumber: string;

constructor(
organizationId: string | number,
year: string,
half: string,
name: string | undefined,
cardNumber: string,
) {
this.organizationId = organizationId;
this.year = year;
this.half = half;
this.name = name;
this.cardNumber = cardNumber;
}
}

export class UpdateCardDto {
cardId: string | number;
name: string | undefined;
cardNumber: string;

constructor(
cardId: string | number,
name: string | undefined,
cardNumber: string,
) {
this.cardId = cardId;
this.name = name;
this.cardNumber = cardNumber;
}
}

export class DeleteCardDto {
cardId: string | number;

constructor(cardId: string | number) {
this.cardId = cardId;
}
}
1 change: 1 addition & 0 deletions src/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './expense.request';
export * from './user.request';
export * from './transaction.request';
export * from './account.request';
export * from './card.request';
52 changes: 52 additions & 0 deletions src/model/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../db';

class Card extends Model {
declare id: number;
declare year: number;
declare half: string;
declare name: string; // 카드 이름
declare cardNumber: string; // 카드번호
declare OrganizationId: number;
}

Card.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
},
year: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
len: [4, 4],
},
},
half: {
type: DataTypes.ENUM('spring', 'fall'),
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: true,
},
cardNumber: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
tableName: 'cards',
sequelize,
indexes: [
{
unique: true,
fields: ['year', 'half', 'OrganizationId'],
},
],
},
);

export default Card;
2 changes: 2 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Expense from './expense';
import Transaction from './transaction';
import AuditPeriod from './audit_period';
import Account from './account';
import Card from './card';

export {
Organization,
Expand All @@ -16,4 +17,5 @@ export {
Transaction,
AuditPeriod,
Account,
Card,
};
56 changes: 56 additions & 0 deletions src/repository/card.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
CreateCardDto,
DeleteCardDto,
GetCardDto,
UpdateCardDto,
} from '../dto';
import { Card } from '../model';

class CardRepository {
async findAll(dto: GetCardDto) {
const cards = await Card.findAll({
where: {
OrganizationId: dto.organizationId,
year: dto.year,
half: dto.half,
},
order: [['cardNumber', 'ASC']],
});
return cards;
}

async create(dto: CreateCardDto) {
const card = await Card.create({
year: dto.year,
half: dto.half,
name: dto.name,
cardNumber: dto.cardNumber,
OrganizationId: dto.organizationId,
});
return card;
}

async update(dto: UpdateCardDto) {
await Card.update(
{
name: dto.name,
cardNumber: dto.cardNumber,
},
{
where: {
id: dto.cardId,
},
},
);
}

async delete(dto: DeleteCardDto) {
await Card.destroy({
where: {
id: dto.cardId,
},
});
}
}

export { CardRepository };
1 change: 1 addition & 0 deletions src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './expense.repository';
export * from './user.repository';
export * from './transaction.repository';
export * from './account.repository';
export * from './card.repository';
27 changes: 27 additions & 0 deletions src/routes/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from 'express';
import { validateOrganization } from '../middleware/auth';
import { wrapAsync } from '../middleware';
import { CardController } from '../controller';

export function createCardsRouter() {
const router = express.Router();
const cardController = new CardController();

router.use(wrapAsync(validateOrganization));

router.get(
'/:organization_id/:year/:half',
wrapAsync(cardController.findAll),
);

router.post(
'/:organization_id/:year/:half',
wrapAsync(cardController.create),
);

router.put('/:card_id', wrapAsync(cardController.update));

router.delete('/:card_id', wrapAsync(cardController.delete));

return router;
}
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { createTransactionsRouter } from './transaction';
import { createTestRouter } from './test';
import { createAccountsRouter } from './account';
import { createBudgetsRouter } from './budget';
import { createCardsRouter } from './card';

export function createRouter() {
const router = express.Router();
router.use('/accounts', createAccountsRouter());
router.use('/budgets', createBudgetsRouter());
router.use('/cards', createCardsRouter());
router.use('/organizations', createOrganizationsRouter());
router.use('/transactions', createTransactionsRouter());
router.use('/users', createUsersRouter());
Expand Down
31 changes: 31 additions & 0 deletions src/service/card.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
CreateCardDto,
DeleteCardDto,
GetCardDto,
UpdateCardDto,
} from '../dto';
import { CardRepository } from '../repository';

class CardService {
private cardRepository: CardRepository = new CardRepository();

async findAll(dto: GetCardDto) {
const cards = await this.cardRepository.findAll(dto);
return cards;
}

async create(dto: CreateCardDto) {
const card = await this.cardRepository.create(dto);
return card;
}

async update(dto: UpdateCardDto) {
await this.cardRepository.update(dto);
}

async delete(dto: DeleteCardDto) {
await this.cardRepository.delete(dto);
}
}

export { CardService };
1 change: 1 addition & 0 deletions src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './expense.service';
export * from './user.service';
export * from './transaction.service';
export * from './account.service';
export * from './card.service';
Loading

0 comments on commit b704fad

Please sign in to comment.