Commit 779dc0af authored by KangMin An's avatar KangMin An
Browse files

Update: DB 모델 변수명 수정 및 모델 추가.사용자 정보 추가 반환.

parent 1919dcb1
...@@ -17,7 +17,7 @@ const handleOutData = async (locCode, date, lat, lng) => { ...@@ -17,7 +17,7 @@ const handleOutData = async (locCode, date, lat, lng) => {
const press = json["main"]["pressure"]; const press = json["main"]["pressure"];
const wind_speed = json["wind"]["speed"]; const wind_speed = json["wind"]["speed"];
await db.Weather_out.create( await db.Weather_Out.create(
{ {
loc_code: Number(locCode), loc_code: Number(locCode),
collected_at: date, collected_at: date,
...@@ -34,7 +34,7 @@ const handleOutData = async (locCode, date, lat, lng) => { ...@@ -34,7 +34,7 @@ const handleOutData = async (locCode, date, lat, lng) => {
// 내부 수집기로 부터 들어온 정보 처리 // 내부 수집기로 부터 들어온 정보 처리
const handleInData = async (email, date, temp, humi, lights) => { const handleInData = async (email, date, temp, humi, lights) => {
await db.Weather_in.create( await db.Weather_In.create(
{ {
host: email, host: email,
collected_at: date, collected_at: date,
...@@ -92,12 +92,12 @@ export const getUserWeatherData = (req, res) => { ...@@ -92,12 +92,12 @@ export const getUserWeatherData = (req, res) => {
try { try {
/* 사용자 email에 따른 사용자 날씨 데이터 가져오기 */ /* 사용자 email에 따른 사용자 날씨 데이터 가져오기 */
const decoded = jwt.decode(acs_token); const decoded = jwt.decode(acs_token);
const result = db.Weather_in.findAll({ const result = db.Weather_In.findAll({
where: { host: decoded.email }, where: { host: decoded.email },
logging: false, logging: false,
}); });
res.json({ msg: resForm.msg.ok, contents: { weather_user: result } }); res.json({ msg: resForm.msg.ok, contents: { weather_in: result } });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.json({ msg: resForm.msg.err, contents: { error: err } }); res.json({ msg: resForm.msg.err, contents: { error: err } });
...@@ -111,7 +111,7 @@ export const getOutWeatherData = (req, res) => { ...@@ -111,7 +111,7 @@ export const getOutWeatherData = (req, res) => {
} = req; } = req;
try { try {
// 실외 지역 번호를 통해 날씨 데이터 전송. // 실외 지역 번호를 통해 날씨 데이터 전송.
const result = db.Weather_out.findAll({ const result = db.Weather_Out.findAll({
where: { loc_code: loc_code }, where: { loc_code: loc_code },
logging: false, logging: false,
}); });
......
...@@ -150,9 +150,10 @@ export const getConfirm = async (req, res) => { ...@@ -150,9 +150,10 @@ export const getConfirm = async (req, res) => {
subject: "userInfo", subject: "userInfo",
}); });
res res.cookie("acs_token", accessT).redirect("/api");
.cookie("acs_token", accessT) // .redirect(
.redirect(`${envs.client.host}:${envs.client.port}`); // `${envs.client.protocol}://${envs.client.host}:${envs.client.port}`
// );
} catch (err) { } catch (err) {
res.json({ msg: resForm.msg.err, contents: { error: err } }); res.json({ msg: resForm.msg.err, contents: { error: err } });
} }
...@@ -164,11 +165,56 @@ export const getUserInfo = async (req, res) => { ...@@ -164,11 +165,56 @@ export const getUserInfo = async (req, res) => {
cookies: { acs_token }, cookies: { acs_token },
} = req; } = req;
try {
const decoded = jwt.decode(acs_token); const decoded = jwt.decode(acs_token);
const result = await db.User.findAll({ where: { email: decoded.email } }); const result_user = await db.User.findAll({
where: { email: decoded.email },
logging: false,
});
let user = {
email: result_user[0].email,
nick_name: result_user[0].nick_name,
using_aircon: result_user[0].using_aircon,
created_at: result_user[0].created_at,
loc_code: result_user[0].loc_code,
};
if (user.loc_code) {
let loc_name = {};
res.status(resForm.code.ok).json({ user_info: result }); const result_emd = await db.Emd.findAll({
where: {
code_emd: user.loc_code,
},
logging: false,
});
const result_doe = await db.Doe.findAll({
where: {
code_doe: result_emd[0].code_doe,
},
logging: false,
});
const result_sgg = await db.Sgg.findAll({
where: {
code_sgg: result_emd[0].code_sgg,
},
logging: false,
});
loc_name.doe = result_doe[0].name_doe;
loc_name.sgg = result_sgg[0].name_sgg;
loc_name.emd = result_emd[0].name_emd;
user.loc_name = loc_name;
}
res.json({ msg: resForm.msg.ok, contents: { user_info: [user] } });
} catch (err) {
console.log(err);
res.json({ msg: resForm.msg.err, contents: { error: err } });
}
}; };
// 사용자 정보 수정 요청 처리 // 사용자 정보 수정 요청 처리
...@@ -178,14 +224,40 @@ export const postEditProfile = async (req, res) => { ...@@ -178,14 +224,40 @@ export const postEditProfile = async (req, res) => {
body: { nick_name, loc_code, using_aircon }, body: { nick_name, loc_code, using_aircon },
} = req; } = req;
try {
const decoded = jwt.decode(acs_token); const decoded = jwt.decode(acs_token);
const result_preuser = await db.User.findAll({
where: { email: decoded.email },
logging: false,
});
const user = result_preuser[0];
let new_nick_name = nick_name ? nick_name : user.nick_name;
let new_loc_code = loc_code ? Number(loc_code) : Number(user.loc_code);
let new_using_aircon = using_aircon
? using_aircon === "true"
: user.using_aircon;
await db.User.update( await db.User.update(
{ nick_name: nick_name, loc_code: loc_code, using_aircon }, {
nick_name: new_nick_name,
loc_code: new_loc_code,
using_aircon: new_using_aircon,
},
{ where: { email: decoded.email } } { where: { email: decoded.email } }
); );
const result = await db.User.findAll({ where: { email: decoded.email } }); const result_after_user = await db.User.findAll({
where: { email: decoded.email },
});
res.json({ msg: resForm.msg.ok, contents: { user_info: result } }); res.json({
msg: resForm.msg.ok,
contents: { user_info: result_after_user },
});
} catch (err) {
console.log(err);
res.json({ msg: resForm.msg.err, contents: { error: err } });
}
}; };
...@@ -4,8 +4,9 @@ import Doe from "../models/doe"; ...@@ -4,8 +4,9 @@ import Doe from "../models/doe";
import Sgg from "../models/sgg"; import Sgg from "../models/sgg";
import Emd from "../models/emd"; import Emd from "../models/emd";
import User from "../models/user"; import User from "../models/user";
import Weather_in from "../models/weather_in"; import Weather_In from "../models/weather_in";
import Weather_out from "../models/weather_out"; import Weather_Out from "../models/weather_out";
import Data_Processing from "../models/data_processing";
// DB의 정보를 모두 담고 있는 객체 생성 // DB의 정보를 모두 담고 있는 객체 생성
const db = {}; const db = {};
...@@ -37,19 +38,23 @@ Emd.init(sequelize); ...@@ -37,19 +38,23 @@ Emd.init(sequelize);
db.User = User; db.User = User;
User.init(sequelize); User.init(sequelize);
db.Weather_in = Weather_in; db.Weather_In = Weather_In;
Weather_in.init(sequelize); Weather_In.init(sequelize);
db.Weather_out = Weather_out; db.Weather_Out = Weather_Out;
Weather_out.init(sequelize); Weather_Out.init(sequelize);
db.Data_Processing = Data_Processing;
Data_Processing.init(sequelize);
// model들 간에 Association 생성 // model들 간에 Association 생성
Doe.associate(db); Doe.associate(db);
Sgg.associate(db); Sgg.associate(db);
Emd.associate(db); Emd.associate(db);
User.associate(db); User.associate(db);
Weather_in.associate(db); Weather_In.associate(db);
Weather_out.associate(db); Weather_Out.associate(db);
Data_Processing.associate(db);
// Messages for Data Base. // Messages for Data Base.
const msg = { const msg = {
......
import { DataTypes, Model } from "sequelize";
import User from "./user";
export class Data_Processing extends Model {
static init(sequelize) {
return super.init(
{
host: {
type: DataTypes.STRING(320),
primaryKey: true,
references: {
model: User,
key: "email",
},
},
collected_at: {
type: DataTypes.DATE,
primaryKey: true,
defaultValue: Date.now(),
},
params: {
type: DataTypes.JSON,
},
},
{
sequelize,
timestamps: false,
paranoid: false,
}
);
}
static associate(db) {
db.Data_Processing.belongsTo(db.User, {
foreignKey: "host",
targetKey: "email",
});
}
}
export default Data_Processing;
...@@ -49,7 +49,7 @@ export class Emd extends Model { ...@@ -49,7 +49,7 @@ export class Emd extends Model {
onDelete: "CASCADE", onDelete: "CASCADE",
onUpdate: "CASCADE", onUpdate: "CASCADE",
}); });
db.Emd.hasMany(db.Weather_out, { db.Emd.hasMany(db.Weather_Out, {
foreignKey: "loc_code", foreignKey: "loc_code",
sourceKey: "code_emd", sourceKey: "code_emd",
onDelete: "CASCADE", onDelete: "CASCADE",
......
...@@ -47,9 +47,15 @@ export class User extends Model { ...@@ -47,9 +47,15 @@ export class User extends Model {
}); });
// User 모델을 참조하는 테이블에 대한 외래키 설정. // User 모델을 참조하는 테이블에 대한 외래키 설정.
db.User.hasMany(db.Weather_in, { db.User.hasMany(db.Weather_In, {
foreignKey: "host", foreignKey: "host",
sourveKey: "email", sourceKey: "email",
onDelete: "CASCADE",
onUpdate: "CASCADE",
});
db.User.hasMany(db.Data_Processing, {
foreignKey: "host",
sourceKey: "email",
onDelete: "CASCADE", onDelete: "CASCADE",
onUpdate: "CASCADE", onUpdate: "CASCADE",
}); });
......
...@@ -40,7 +40,7 @@ export class Weather_In extends Model { ...@@ -40,7 +40,7 @@ export class Weather_In extends Model {
static associate(db) { static associate(db) {
// weather_in 모델이 참조하는 테이블에 대한 외래키 설정. // weather_in 모델이 참조하는 테이블에 대한 외래키 설정.
db.Weather_in.belongsTo(db.User, { db.Weather_In.belongsTo(db.User, {
foreignKey: "host", foreignKey: "host",
targetKey: "email", targetKey: "email",
}); });
......
...@@ -44,7 +44,7 @@ export class Weather_Out extends Model { ...@@ -44,7 +44,7 @@ export class Weather_Out extends Model {
static associate(db) { static associate(db) {
// weather_out 모델이 참조하는 테이블에 대한 외래키 설정. // weather_out 모델이 참조하는 테이블에 대한 외래키 설정.
db.Weather_out.belongsTo(db.Emd, { db.Weather_Out.belongsTo(db.Emd, {
foreignKey: "loc_code", foreignKey: "loc_code",
targetKey: "code_emd", targetKey: "code_emd",
}); });
......
block content block content
.signup-form_container .signup-form_container
form(action = routes.base + routes.editProfile, method = "post") form(action = routes.base + routes.editProfile, method = "post")
input(type="text", name="nick_name", require=true, placeholder="Nick Name") input(type="text", name="nick_name", placeholder="Nick Name")
input(type="number", name="loc_code", require=true, placeholder="Local Code.") input(type="text", name="using_aircon", placeholder="Using Aircon? true or false" )
input(type="number", name="loc_code", placeholder="Local Code.")
input(type="submit", value="Submit") input(type="submit", value="Submit")
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment