Skip to content

Commit

Permalink
Merge pull request #25 from TeamShortWeather/feature/#22
Browse files Browse the repository at this point in the history
Feature/#22
  • Loading branch information
coreminw authored Jan 8, 2023
2 parents 53b3110 + 7c6c54c commit 71eb074
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 118 deletions.
79 changes: 61 additions & 18 deletions src/controllers/WeatherController.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,79 @@
import { Request, Response } from "express";
import WeatherService from "../services/WeatherService";
import sc from "../modules/statusCode";
import rm from "../modules/responseMessage";
import util from "../modules/util"
import util from "../modules/util";
import WeatherService from "../services/WeatherService";

const getWeatherDetail = async (req: Request, res: Response) => {
const data = await WeatherService.getWeatherDetail();
/**
* @route GET /weather/today/detail/rain
* @desc Get Schedule
* @access public
* @description 시간대별 날씨 - 강수 조회
* @developer 김민욱
*/
const getRainForecast = async (req: Request, res: Response) => {
try {
const data = await WeatherService.getRainForecast();

if (!data) return res.status(sc.BAD_REQUEST).send(util.fail(sc.BAD_REQUEST, rm.READ_OBSERVED_WEATHER_FAIL));
if (!data) {
return res
.status(sc.BAD_REQUEST)
.send(util.fail(sc.BAD_REQUEST, rm.READ_OBSERVED_WEATHER_FAIL));
}

return res.status(sc.OK).send(util.success(sc.OK, rm.READ_OBSERVED_WEATHER_SUCCESS, data));
return res
.status(sc.OK)
.send(util.success(sc.OK, rm.READ_RAIN_WEATHER_SUCCESS, data));
} catch (error) {
console.log(error);
res
.status(sc.INTERNAL_SERVER_ERROR)
.send(util.fail(sc.INTERNAL_SERVER_ERROR, rm.INTERNAL_SERVER_ERROR));
}
};

const getTempForecast = async (req: Request, res: Response) => {
const data = await WeatherService.getTempForecast();
/**
* @route GET /weather/today/detail
* @desc Get Weather
* @access public
* @description 날씨 디테일
* @developer 김도연
*/
const getWeatherDetail = async (req: Request, res: Response) => {
const data = await WeatherService.getWeatherDetail();

if (!data) return res.status(sc.BAD_REQUEST).send(util.fail(sc.BAD_REQUEST, rm.READ_OBSERVED_WEATHER_FAIL));
if (!data)
return res
.status(sc.BAD_REQUEST)
.send(util.fail(sc.BAD_REQUEST, rm.READ_OBSERVED_WEATHER_FAIL));

return res.status(sc.OK).send(util.success(sc.OK, rm.READ_OBSERVED_WEATHER_SUCCESS, data));
return res
.status(sc.OK)
.send(util.success(sc.OK, rm.READ_OBSERVED_WEATHER_SUCCESS, data));
};

const getRainForecast = async (req: Request, res: Response) => {
const data = await WeatherService.getRainForecast();
/**
* @route GET /weather/today/detail/temp
* @desc Get Weather
* @access public
* @description 시간대별 날씨 온도 조회
* @developer 김도연
*/
const getTempForecast = async (req: Request, res: Response) => {
const data = await WeatherService.getTempForecast();

if (!data) return res.status(sc.BAD_REQUEST).send(util.fail(sc.BAD_REQUEST, rm.READ_OBSERVED_WEATHER_FAIL));
if (!data)
return res
.status(sc.BAD_REQUEST)
.send(util.fail(sc.BAD_REQUEST, rm.READ_OBSERVED_WEATHER_FAIL));

return res.status(sc.OK).send(util.success(sc.OK, rm.READ_OBSERVED_WEATHER_SUCCESS, data));
return res
.status(sc.OK)
.send(util.success(sc.OK, rm.READ_OBSERVED_WEATHER_SUCCESS, data));
};

export default {
getWeatherDetail,
getTempForecast,
getRainForecast
getWeatherDetail,
getTempForecast,
getRainForecast,
};

3 changes: 2 additions & 1 deletion src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AuthController from './AuthController';
import ScheduleController from './ScheduleController';
import WeatherController from './WeatherController';

export { AuthController, ScheduleController };
export { AuthController, ScheduleController, WeatherController };
58 changes: 30 additions & 28 deletions src/modules/responseMessage.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
const message = {
NULL_VALUE: '필요한 값이 없습니다.',
NOT_FOUND: '존재하지 않는 자원',
BAD_REQUEST: '잘못된 요청',
INTERNAL_SERVER_ERROR: '서버 내부 오류',
SUCCESS: '성공',
NULL_VALUE_TOKEN: '토큰만료',
BAD_PATH: '잘못된 경로입니다.',
UNAUTHORIZED: '승인되지 않은 유저입니다.',
FORBIDDEN: '권한이 없는 유저의 요청입니다.',
DUPLICATED: '이미 존재하는 데이터입니다.',
TEMPORARY_UNAVAILABLE: '일시적으로 사용할 수 없는 서버입니다.',
DB_ERROR: '데이터베이스 오류입니다.',
NULL_VALUE: "필요한 값이 없습니다.",
NOT_FOUND: "존재하지 않는 자원",
BAD_REQUEST: "잘못된 요청",
INTERNAL_SERVER_ERROR: "서버 내부 오류",
SUCCESS: "성공",
NULL_VALUE_TOKEN: "토큰만료",
BAD_PATH: "잘못된 경로입니다.",
UNAUTHORIZED: "승인되지 않은 유저입니다.",
FORBIDDEN: "권한이 없는 유저의 요청입니다.",
DUPLICATED: "이미 존재하는 데이터입니다.",
TEMPORARY_UNAVAILABLE: "일시적으로 사용할 수 없는 서버입니다.",
DB_ERROR: "데이터베이스 오류입니다.",

CREATE_USER: '유저 생성 성공',
SUCCESS_SIGN_OUT: '로그아웃 성공',
SUCCESS_WITHDRAWAL: '회원탈퇴 성공',
TOKEN_EMPTY: '토큰이 없습니다.',
TOKEN_INVALID: '토큰이 유효하지 않음',
CREATE_TOKEN: '토큰 생성',
ALL_TOKEN_EXPIRED: '모든 토큰이 만료되었음',
TOKEN_EXPIRED: '토큰 만료',
CREATE_DEVICE_SUCCESS: '디바이스 회원가입 성공',
DEVICE_LOGIN_SUCCESS: '디바이스 로그인 성공',
CREATE_USER: "유저 생성 성공",
SUCCESS_SIGN_OUT: "로그아웃 성공",
SUCCESS_WITHDRAWAL: "회원탈퇴 성공",
TOKEN_EMPTY: "토큰이 없습니다.",
TOKEN_INVALID: "토큰이 유효하지 않음",
CREATE_TOKEN: "토큰 생성",
ALL_TOKEN_EXPIRED: "모든 토큰이 만료되었음",
TOKEN_EXPIRED: "토큰 만료",
CREATE_DEVICE_SUCCESS: "디바이스 회원가입 성공",
DEVICE_LOGIN_SUCCESS: "디바이스 로그인 성공",

READ_OBSERVED_WEATHER_SUCCESS: '관측 날씨 조회 성공',
READ_OBSERVED_WEATHER_FAIL: '관측 날씨 조회 실패',
READ_OBSERVED_WEATHER_SUCCESS: "관측 날씨 조회 성공",
READ_OBSERVED_WEATHER_FAIL: "관측 날씨 조회 실패",

CREATE_USER_SUCCESS: '유저 정보 입력 성공',
CREATE_USER_FAIL: '유저 정보 입력 실패',
READ_USER_SUCCESS: '유저 등록 조회 성공',
READ_USER_UNAUTHORIZED: '등록되지 않은 유저입니다.',
CREATE_USER_SUCCESS: "유저 정보 입력 성공",
CREATE_USER_FAIL: "유저 정보 입력 실패",
READ_USER_SUCCESS: "유저 등록 조회 성공",
READ_USER_UNAUTHORIZED: "등록되지 않은 유저입니다.",

READ_RAIN_WEATHER_SUCCESS: "시간대별 강수 조회 성공",
};

export default message;
1 change: 1 addition & 0 deletions src/routes/WeatherRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const router = Router();

router.get('/today/detail', WeatherController.getWeatherDetail);
router.get('/today/detail/temp', WeatherController.getTempForecast);
router.get("/today/detail/rain", WeatherController.getRainForecast);

export default router;
158 changes: 88 additions & 70 deletions src/services/WeatherService.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,104 @@
import { PrismaClient } from "@prisma/client";
import { WeatherInfoDTO } from '../DTO/WeatherDTO';
import dayjs from "dayjs";
import { database } from "firebase-admin";

const prisma = new PrismaClient();
const moment = require("moment");
require("moment-timezone");
moment.tz.setDefault("Asia/Seoul");
moment.suppressDeprecationWarnings = true;

let now = moment();
let date = now.format("YYYYMMDD");
let time = now.format("HH00");

let now = dayjs();
const date = now.format("YYYYMMDD");
const time = now.format("HH00");
const getRainForecast = async () => {
const start = await prisma.hourly_forecast.findFirst({
where: {
date: date,
time: time,
},
select: {
id: true,
},
});

const result = await prisma.hourly_forecast.findMany({
where: {
id: {
gt: start.id - 1,
},
},
select: {
time: true,
rain: true,
},
take: 24,
});
return result;
};

const getWeatherDetail = async () => {
const user = await prisma.user.findUnique({
where: {
id: 1,
},
});
const observed = await prisma.observed_weather.findFirst();
const daily = await prisma.daily_forecast.findFirst();

const user = await prisma.user.findUnique({
where: {
id: 1,
},
});
const observed = await prisma.observed_weather.findFirst();
const daily = await prisma.daily_forecast.findFirst();

const data = {
goOut: {
time: user.go_out_time
},
goHome: {
time: user.go_home_time
},
todayWeather: {
humidity: observed.humidity,
surise: daily.sunrise,
sunset: daily.sunset,
fineDust: observed.pm25,
ultraFineDust: observed.pm10
}
}
const data = {
goOut: {
time: user.go_out_time,
},
goHome: {
time: user.go_home_time,
},
todayWeather: {
humidity: observed.humidity,
surise: daily.sunrise,
sunset: daily.sunset,
fineDust: observed.pm25,
ultraFineDust: observed.pm10,
},
};

return data;
return data;
};

const getTempForecast = async () => {
const start = await prisma.hourly_forecast.findFirst({
where: {
date: date,
time: time,
},
select: {
id: true,
},
})
const start = await prisma.hourly_forecast.findFirst({
where: {
date: date,
time: time,
},
select: {
id: true,
},
});

const result = await prisma.hourly_forecast.findMany({
where: {
id: {
gt: start.id-1,
}
},
select: {
id: true,
date: true,
time: true,
temperature: true,
sky: true,
pty: true,
},
take: 24,
})
const result = await prisma.hourly_forecast.findMany({
where: {
id: {
gt: start.id - 1,
},
},
select: {
id: true,
date: true,
time: true,
temperature: true,
sky: true,
pty: true,
},
take: 24,
});

console.log(result.length)
return result;
console.log(result.length);
return result;
};

const getRainForecast = async () => {

const data = {}

return data;
const WeatherService = {
getWeatherDetail,
getTempForecast,
getRainForecast,
};

const WeatherService = {
getWeatherDetail,
getTempForecast,
getRainForecast
}

export default WeatherService;
export default WeatherService;
3 changes: 2 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AuthService from "./AuthService";
import ScheduleService from "./ScheduleService";
export default { AuthService, ScheduleService };
import WeatherService from "./WeatherService";
export default { AuthService, ScheduleService, WeatherService };

0 comments on commit 71eb074

Please sign in to comment.