-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
544 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.