Skip to content

Commit

Permalink
refactor: 변수명 pin 사용으로 통일 #255
Browse files Browse the repository at this point in the history
  • Loading branch information
Miensoap committed Dec 2, 2024
1 parent 0e72d76 commit cd4602e
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 105 deletions.
4 changes: 2 additions & 2 deletions backend/src/course/CourseRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class CourseRepository extends SoftDeleteRepository<Course, number> {
return this.update(id, { title, description, thumbnailUrl });
}

async updateCoursePlaceById(course: Course) {
async updatePins(course: Course) {
await this.coursePlaceRepository
.createQueryBuilder()
.delete()
Expand All @@ -77,7 +77,7 @@ export class CourseRepository extends SoftDeleteRepository<Course, number> {
.createQueryBuilder()
.insert()
.into(CoursePlace)
.values(course.coursePlaces)
.values(course.pins)
.orUpdate(['order'], ['course_id', 'place_id'])
.execute();
}
Expand Down
17 changes: 6 additions & 11 deletions backend/src/course/CourseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { PagedCourseResponse } from '@src/course/dto/PagedCourseResponse';
import { CreateCourseRequest } from '@src/course/dto/CreateCourseRequest';
import { UpdateCourseInfoRequest } from '@src/course/dto/UpdateCourseInfoRequest';
import { UpdatePinsOfCourseRequest } from '@src/course/dto/AddPlaceToCourseRequest';
import { UpdatePinsOfCourseRequest } from '@src/course/dto/UpdatePinsOfCourseRequest';
import { CourseNotFoundException } from '@src/course/exception/CourseNotFoundException';
import { PlaceRepository } from '@src/place/PlaceRepository';
import { InvalidPlaceToCourseException } from '@src/course/exception/InvalidPlaceToCourseException';
Expand Down Expand Up @@ -102,18 +102,13 @@ export class CourseService {
}

@Transactional()
async updatePins(
id: number,
setPlacesOfCourseRequest: UpdatePinsOfCourseRequest,
) {
async updatePins(id: number, request: UpdatePinsOfCourseRequest) {
const course = await this.getCourseOrElseThrowNotFound(id);

await this.validatePlacesForCourse(
setPlacesOfCourseRequest.places.map((p) => p.placeId),
);
await this.validatePlacesForCourse(request.pins.map((pin) => pin.placeId));

course.setPlaces(setPlacesOfCourseRequest.places);
await this.courseRepository.updateCoursePlaceById(course);
course.setPins(request.pins);
await this.courseRepository.updatePins(course);
const reloadedCourse = await this.courseRepository.findById(course.id);

return {
Expand All @@ -125,7 +120,7 @@ export class CourseService {
async updatePin(id: number, placeId: number, comment?: string) {
const course = await this.getCourseOrElseThrowNotFound(id);

course.updatePlace(placeId, comment);
course.updatePin(placeId, comment);
return this.courseRepository.save(course);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/course/dto/CourseDetailResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CourseDetailResponse {
}

export async function getPlacesResponseOfCourseWithOrder(course: Course) {
return (await course.getPlacesWithComment()).map((place, index) => {
return (await course.getPinsWithComment()).map((place, index) => {
return {
...PlaceListResponse.from(place.place),
comment: place.comment,
Expand Down
40 changes: 19 additions & 21 deletions backend/src/course/entity/Course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { BaseEntity } from '@src/common/BaseEntity';
import { User } from '@src/user/entity/User';
import { CoursePlace } from '@src/course/entity/CoursePlace';
import { SetPlacesOfCourseRequestItem } from '@src/course/dto/AddPlaceToCourseRequest';
import { UpdatePinsOfCourseRequestItem } from '@src/course/dto/UpdatePinsOfCourseRequest';
import { PlaceInCourseNotFoundException } from '@src/course/exception/PlaceInCourseNotFoundException';

@Entity()
Expand Down Expand Up @@ -31,7 +31,7 @@ export class Course extends BaseEntity {
eager: true,
cascade: true,
})
coursePlaces: CoursePlace[];
pins: CoursePlace[];

constructor(
user: User,
Expand All @@ -49,39 +49,37 @@ export class Course extends BaseEntity {
}

get pinCount() {
if (!this.coursePlaces) return 0;
return this.coursePlaces.length;
if (!this.pins) return 0;
return this.pins.length;
}

setPlaces(coursePlaces: SetPlacesOfCourseRequestItem[]) {
this.coursePlaces = coursePlaces.map((item, index) => {
setPins(pins: UpdatePinsOfCourseRequestItem[]) {
this.pins = pins.map((item, index) => {
return CoursePlace.of(index + 1, item.placeId, this, item.comment);
});
}

getPlace(placeId: number) {
const coursePlace = this.coursePlaces.find(
(coursePlace) => coursePlace.placeId === placeId,
);
if (!coursePlace) {
getPin(placeId: number) {
const pin = this.pins.find((pin) => pin.placeId === placeId);
if (!pin) {
throw new PlaceInCourseNotFoundException(this.id, placeId);
}
return coursePlace;
return pin;
}

updatePlace(placeId: number, comment?: string) {
const updated = this.getPlace(placeId).update(comment);
this.coursePlaces = this.coursePlaces.map((coursePlace) =>
coursePlace.placeId === placeId ? updated : coursePlace,
updatePin(placeId: number, comment?: string) {
const updated = this.getPin(placeId).update(comment);
this.pins = this.pins.map((pin) =>
pin.placeId === placeId ? updated : pin,
);
}

async getPlacesWithComment() {
const coursePlaces = this.coursePlaces.sort((a, b) => a.order - b.order);
async getPinsWithComment() {
const pins = this.pins.sort((a, b) => a.order - b.order);
return await Promise.all(
coursePlaces.map(async (coursePlace) => ({
place: await coursePlace.place,
comment: coursePlace.description,
pins.map(async (pin) => ({
place: await pin.place,
comment: pin.description,
})),
);
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/course/pipes/IsNotConsecutiveDuplicatePlace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
import { SetPlacesOfCourseRequestItem } from '@src/course/dto/AddPlaceToCourseRequest';
import { UpdatePinsOfCourseRequestItem } from '@src/course/dto/UpdatePinsOfCourseRequest';
import { ConsecutivePlaceException } from '@src/course/exception/ConsecutivePlaceException';

@ValidatorConstraint({ name: 'isNotConsecutiveDuplicatePlace', async: false })
export class IsNotConsecutiveDuplicatePlace
implements ValidatorConstraintInterface
{
validate(places: SetPlacesOfCourseRequestItem[]) {
validate(places: UpdatePinsOfCourseRequestItem[]) {
for (let i = 1; i < places.length; i++) {
if (places[i].placeId === places[i - 1].placeId) {
throw new ConsecutivePlaceException();
Expand Down
16 changes: 8 additions & 8 deletions backend/src/map/MapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { CreateMapRequest } from '@src/map/dto/CreateMapRequest';
import { UpdateMapInfoRequest } from '@src/map/dto/UpdateMapInfoRequest';
import { AddPinToMapRequest } from '@src/map/dto/AddPinToMapRequest';
import { UpdateMapVisibilityRequest } from '@src/map/dto/UpdateMapVisibilityRequest';
import { UpdatePinInMapRequest } from '@src/map/dto/UpdatePinInMapRequest';
import { UpdatePinInfoInMapRequest } from '@src/map/dto/UpdatePinInfoInMapRequest';
import { ParseOptionalNumberPipe } from '@src/common/pipe/ParseOptionalNumberPipe';
import { MapPermissionGuard } from '@src/map/guards/MapPermissionGuard';
import { EmptyRequestException } from '@src/common/exception/EmptyRequestException';
Expand Down Expand Up @@ -60,26 +60,26 @@ export class MapController {

@UseGuards(JwtAuthGuard, MapPermissionGuard)
@Post('/:id/places')
async addPlaceToMap(
async addPinToMap(
@Param('id') id: number,
@Body() addPinToMapRequest: AddPinToMapRequest,
) {
return await this.mapService.addPlace(id, addPinToMapRequest);
return await this.mapService.addPin(id, addPinToMapRequest);
}

@UseGuards(JwtAuthGuard, MapPermissionGuard)
@Put('/:id/places/:placeId')
async updatePinInMap(
async updatePinInfoInMap(
@Param('id') id: number,
@Param('placeId') placeId: number,
@Body() updatePinInMapRequest: UpdatePinInMapRequest,
@Body() updatePinInfoInMapRequest: UpdatePinInfoInMapRequest,
) {
if (updatePinInMapRequest.isEmpty()) {
if (updatePinInfoInMapRequest.isEmpty()) {
throw new EmptyRequestException();
}

await this.mapService.updatePin(id, placeId, updatePinInMapRequest);
return { mapId: id, placeId, ...updatePinInMapRequest };
await this.mapService.updatePin(id, placeId, updatePinInfoInMapRequest);
return { mapId: id, placeId, ...updatePinInfoInMapRequest };
}

@UseGuards(JwtAuthGuard, MapPermissionGuard)
Expand Down
16 changes: 10 additions & 6 deletions backend/src/map/MapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { InvalidPlaceToMapException } from '@src/map/exception/InvalidPlaceToMap
import { User } from '@src/user/entity/User';
import { PlaceRepository } from '@src/place/PlaceRepository';
import { AddPinToMapRequest } from '@src/map/dto/AddPinToMapRequest';
import { UpdatePinInMapRequest } from '@src/map/dto/UpdatePinInMapRequest';
import { UpdatePinInfoInMapRequest } from '@src/map/dto/UpdatePinInfoInMapRequest';
import { UpdateMapVisibilityRequest } from '@src/map/dto/UpdateMapVisibilityRequest';

@Injectable()
Expand Down Expand Up @@ -104,13 +104,13 @@ export class MapService {
return this.mapRepository.update(id, { isPublic: visibility.isPublic });
}

async addPlace(id: number, pinInfo: AddPinToMapRequest) {
async addPin(id: number, pinInfo: AddPinToMapRequest) {
const { placeId, color, comment } = pinInfo;
const map = await this.mapRepository.findById(id);
if (!map) throw new MapNotFoundException(id);
await this.validatePlacesForMap(placeId, map);

map.addPlace(placeId, color, comment);
map.addPin(placeId, color, comment);
await this.mapRepository.save(map);

return {
Expand All @@ -121,18 +121,22 @@ export class MapService {
}

@Transactional()
async updatePin(id: number, placeId: number, pinInfo: UpdatePinInMapRequest) {
async updatePin(
id: number,
placeId: number,
pinInfo: UpdatePinInfoInMapRequest,
) {
const { color, comment } = pinInfo;
const map = await this.getMapOrElseThrowNotFound(id);
map.updatePlace(placeId, color, comment);
map.updatePin(placeId, color, comment);

return this.mapRepository.save(map);
}

async deletePin(id: number, placeId: number) {
const map = await this.getMapOrElseThrowNotFound(id);

map.deletePlace(placeId);
map.deletePin(placeId);
await this.mapRepository.save(map);

return { deletedId: placeId };
Expand Down
2 changes: 1 addition & 1 deletion backend/src/map/dto/MapDetailResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class MapDetailResponse {
) {}

static async from(map: Map) {
const places = (await map.getPlacesWithComment()).map((place) => {
const places = (await map.getPinsWithComment()).map((place) => {
return {
...PlaceListResponse.from(place.place),
comment: place.comment,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IsEnum, IsOptional, IsString } from 'class-validator';
import { Color } from '@src/place/enum/Color';

export class UpdatePinInMapRequest {
export class UpdatePinInfoInMapRequest {
@IsOptional()
@IsEnum(Color)
color?: Color;
Expand Down
40 changes: 20 additions & 20 deletions backend/src/map/entity/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Map extends BaseEntity {
eager: true,
cascade: true,
})
mapPlaces: MapPlace[];
pins: MapPlace[];

constructor(
user: User,
Expand All @@ -48,43 +48,43 @@ export class Map extends BaseEntity {
}

get pinCount() {
return this.mapPlaces.length;
return this.pins.length;
}

addPlace(placeId: number, color: Color, description: string) {
this.mapPlaces.push(MapPlace.of(placeId, this, color, description));
addPin(placeId: number, color: Color, description: string) {
this.pins.push(MapPlace.of(placeId, this, color, description));
}

getPlace(placeId: number) {
const mapPlace = this.mapPlaces.find((p) => p.placeId === placeId);
if (!mapPlace) throw new PlaceInMapNotFoundException(this.id, placeId);
getPin(placeId: number) {
const pin = this.pins.find((p) => p.placeId === placeId);
if (!pin) throw new PlaceInMapNotFoundException(this.id, placeId);

return mapPlace;
return pin;
}

async getPlacesWithComment() {
async getPinsWithComment() {
return await Promise.all(
this.mapPlaces.map(async (mapPlace) => ({
place: await mapPlace.place,
comment: mapPlace.description,
color: mapPlace.color,
this.pins.map(async (pin) => ({
place: await pin.place,
comment: pin.description,
color: pin.color,
})),
);
}

hasPlace(placeId: number) {
return this.mapPlaces.some((p) => p.placeId === placeId);
return this.pins.some((p) => p.placeId === placeId);
}

updatePlace(placeId: number, color?: Color, comment?: string) {
const updated = this.getPlace(placeId).update(color, comment);
updatePin(placeId: number, color?: Color, comment?: string) {
const updated = this.getPin(placeId).update(color, comment);

this.mapPlaces = this.mapPlaces.map((mapPlace) =>
mapPlace.placeId === placeId ? updated : mapPlace,
this.pins = this.pins.map((pin) =>
pin.placeId === placeId ? updated : pin,
);
}

deletePlace(placeId: number) {
this.mapPlaces = this.mapPlaces.filter((p) => p.placeId !== placeId);
deletePin(placeId: number) {
this.pins = this.pins.filter((p) => p.placeId !== placeId);
}
}
2 changes: 1 addition & 1 deletion backend/src/map/entity/MapPlace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class MapPlace extends BaseEntity {
@JoinColumn({ name: 'place_id' })
place: Promise<Place>;

@ManyToOne(() => Map, (map) => map.mapPlaces, {
@ManyToOne(() => Map, (map) => map.pins, {
onDelete: 'CASCADE',
orphanedRowAction: 'delete',
})
Expand Down
8 changes: 4 additions & 4 deletions backend/test/course/course.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { CreateCourseRequest } from '@src/course/dto/CreateCourseRequest';
import { UpdateCourseInfoRequest } from '@src/course/dto/UpdateCourseInfoRequest';
import {
UpdatePinsOfCourseRequest,
SetPlacesOfCourseRequestItem,
} from '@src/course/dto/AddPlaceToCourseRequest';
UpdatePinsOfCourseRequestItem,
} from '@src/course/dto/UpdatePinsOfCourseRequest';
import { InvalidPlaceToCourseException } from '@src/course/exception/InvalidPlaceToCourseException';
import { initializeTransactionalContext } from 'typeorm-transactional';
import { MySqlContainer, StartedMySqlContainer } from '@testcontainers/mysql';
Expand Down Expand Up @@ -316,12 +316,12 @@ describe('CourseService', () => {

describe('코스에 장소를 추가할 때', () => {
const setPlacesOfCourseRequest = {
places: [
pins: [
{
placeId: 1,
comment: 'A popular place',
},
] as SetPlacesOfCourseRequestItem[],
] as UpdatePinsOfCourseRequestItem[],
} as UpdatePinsOfCourseRequest;

it('코스가 존재하지 않으면 예외를 던진다', async () => {
Expand Down
Loading

0 comments on commit cd4602e

Please sign in to comment.