Skip to content

Commit

Permalink
fix: 에세이 작성시 위도,경도 데이터 geometry로 변환
Browse files Browse the repository at this point in the history
  • Loading branch information
daechan-jo committed Oct 24, 2024
1 parent aa7cad2 commit 7f16514
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/modules/bookmark/bookmark.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export class BookmarkController {
2. 성공 상태를 반환합니다.
**주의 사항:**
- 로그인한 사용자가 접근할 수 있습니다.
- 자신의 에세이는 마킹할 수 없습니다.
- \`private\`상태의 에세이는 마킹할 수 없습니다.
`,
})
@ApiResponse({ status: 201 })
Expand Down
7 changes: 5 additions & 2 deletions src/modules/bookmark/bookmark.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ export class BookmarkService {
const essay = await this.essayService.getEssayById(essayId);

if (essay.status === EssayStatus.PRIVATE || essay.author.id === userId)
throw new HttpException('Bad request.', HttpStatus.BAD_REQUEST);
throw new HttpException(
'자신의 에세이 혹은 비공개 에세이는 북마크할 수 없습니다.',
HttpStatus.BAD_REQUEST,
);

const existingBookmark = await this.bookmarkRepository.findBookmark(user, essay);

if (existingBookmark) {
throw new HttpException('Bookmark already exists.', HttpStatus.CONFLICT);
throw new HttpException('이미 북마크한 에세이 입니다.', HttpStatus.CONFLICT);
}

await this.bookmarkRepository.addBookmark(user, essay);
Expand Down
12 changes: 8 additions & 4 deletions src/modules/essay/dto/saveEssay.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
IsNumber,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
import { User } from '../../../entities/user.entity';
import { Story } from '../../../entities/story.entity';
import { Device } from '../../../entities/device.entity';
import { EssayStatus } from '../../../common/types/enum.types';
import { Type } from 'class-transformer';
import { Tag } from '../../../entities/tag.entity';

export class SaveEssayDto {
@IsNotEmpty()
Expand All @@ -25,9 +27,6 @@ export class SaveEssayDto {
@IsNumber()
linkedOutGauge?: number;

@IsOptional()
category?: Story;

@IsOptional()
@IsString()
thumbnail?: string;
Expand All @@ -53,4 +52,9 @@ export class SaveEssayDto {

@IsNotEmpty()
author: User;

@IsOptional()
@ValidateNested({ each: true })
@Type(() => Tag)
tags?: Tag[];
}
42 changes: 38 additions & 4 deletions src/modules/essay/essay.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,41 @@ export class EssayRepository {
}

async saveEssay(data: SaveEssayDto) {
return this.essayRepository.save(data);
const result = await this.essayRepository
.createQueryBuilder()
.insert()
.into('essay')
.values({
title: data.title,
content: data.content,
linkedOutGauge: data.linkedOutGauge,
status: data.status,
device: data.device,
author: data.author,
createdDate: new Date(),
updatedDate: new Date(),
coordinates:
data.longitude && data.latitude
? () => `ST_SetSRID(ST_GeomFromText('POINT(${data.longitude} ${data.latitude})'), 4326)`
: null,
})
.returning('*')
.execute();

const insertedEssayId = result.identifiers[0].id;

const savedEssay = await this.essayRepository.findOne({
where: { id: insertedEssayId },
relations: ['tags', 'author', 'device'],
});

if (data.tags && data.tags.length > 0) {
savedEssay.tags = data.tags;

await this.essayRepository.save(savedEssay);
}

return savedEssay;
}

async saveEssays(essays: Essay[]) {
Expand Down Expand Up @@ -75,7 +109,7 @@ export class EssayRepository {
.leftJoinAndSelect('essay.tags', 'tags')
.where('essay.author.id = :userId', { userId })
.andWhere('essay.status != :linkedOutStatus', { linkedOutStatus: EssayStatus.LINKEDOUT })
.andWhere('essay.status != :buryStatus', { buryStatus: EssayStatus.BURY });
.andWhere('essay.status != :BURIEDStatus', { BURIEDStatus: EssayStatus.BURIED });

if (storyId !== undefined) {
queryBuilder.andWhere('essay.story.id = :storyId', { storyId });
Expand Down Expand Up @@ -116,7 +150,7 @@ export class EssayRepository {
.where('essay.author.id = :userId', { userId })
.andWhere('essay.status != :linkedOutStatus', { linkedOutStatus: EssayStatus.LINKEDOUT })
.andWhere('essay.status != :privateStatus', { privateStatus: EssayStatus.PRIVATE })
.andWhere('essay.status != :buryStatus', { buryStatus: EssayStatus.BURY });
.andWhere('essay.status != :BURIEDStatus', { BURIEDStatus: EssayStatus.BURIED });

if (storyId !== undefined) {
queryBuilder.andWhere('essay.story.id = :storyId', { storyId });
Expand Down Expand Up @@ -160,7 +194,7 @@ export class EssayRepository {
)
.leftJoin('essay.tags', 'tags')
.where('essay.status != :status', { status: EssayStatus.PRIVATE })
.andWhere('essay.status != :buryStatus', { buryStatus: EssayStatus.BURY })
.andWhere('essay.status != :BURIEDStatus', { BURIEDStatus: EssayStatus.BURIED })
.andWhere('essay.deletedDate IS NULL')
.andWhere(
new Brackets((qb) => {
Expand Down
14 changes: 7 additions & 7 deletions src/modules/essay/essay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { SupportService } from '../support/support.service';
import { EssayStatus, PageType, UserStatus } from '../../common/types/enum.types';
import { Aggregate } from '../../entities/aggregate.entity';
import { Request as ExpressRequest } from 'express';
import { SaveEssayDto } from './dto/saveEssay.dto';

@Injectable()
export class EssayService {
Expand All @@ -64,7 +65,7 @@ export class EssayService {
}

@Transactional()
async saveEssay(requester: Express.User, deviceDto: DeviceDto, data: CreateEssayReqDto) {
async saveEssay(requester: Express.User, reqDevice: DeviceDto, data: CreateEssayReqDto) {
if (data.status === EssayStatus.BURIED) {
if (!data.latitude || !data.longitude)
throw new HttpException(
Expand All @@ -75,18 +76,17 @@ export class EssayService {

const user = await this.userService.fetchUserEntityById(requester.id);
const tags = await this.tagService.getTags(data.tags);

let device = await this.supportService.findDevice(user, deviceDto);
let device = await this.supportService.findDevice(user, reqDevice);

if (!device) {
device = await this.supportService.newCreateDevice(user, deviceDto);
device = await this.supportService.newCreateDevice(user, reqDevice);
}

const essayData = {
const essayData: SaveEssayDto = {
...data,
device: device,
device,
author: user,
tags: tags,
tags,
};

void this.badgeService.addExperience(user, tags);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/support/support.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export class SupportService {
model: device.model,
});

await this.redis.del(`user:${user.id}`);

return await this.supportRepository.saveDevice(newDevice);
}

Expand Down
4 changes: 4 additions & 0 deletions src/modules/utils/utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,8 @@ export class UtilsService {

return htmlContent;
}

coordinatesToGeometry(latitude: number, longitude: number): string {
return `ST_SetSRID(ST_GeomFromText('POINT(${longitude} ${latitude})'), 4326)`;
}
}

0 comments on commit 7f16514

Please sign in to comment.