Skip to content

Commit

Permalink
weather model, findandupdate for it
Browse files Browse the repository at this point in the history
  • Loading branch information
farmasek committed Feb 26, 2017
1 parent de5dc81 commit 5844568
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ This project is setup for future QraphQL project.
- [x] Add Editor config
- [x] GraphQL PoC
- [x] Mongoose PoC
- [ ] CRUD manipulations with DB, nested models
- [x] CRUD manipulations with DB, nested models
- [ ] Weather API microservice
42 changes: 42 additions & 0 deletions datasourceexample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export const example = {
"coord": {
"lon": 18.24,
"lat": 49.89
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 279.15,
"pressure": 1015,
"humidity": 48,
"temp_min": 279.15,
"temp_max": 279.15
},
"visibility": 10000,
"wind": {
"speed": 7.7,
"deg": 210
},
"clouds": {
"all": 0
},
"dt": 1488132000,
"sys": {
"type": 1,
"id": 5894,
"message": 0.0039,
"country": "CZ",
"sunrise": 1488087277,
"sunset": 1488126343
},
"id": 3071429,
"name": "Ludgerovice",
"cod": 200
}
12 changes: 12 additions & 0 deletions graphql/schema/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@ export default {
hours.map(hour => hour.hour = 56);
return hours;
}
},
Mutation: {
async insertHour(_, {hour}){
console.log(hour)
try {
const insertedHour = await Hour.create(hour);
return insertedHour;
}
catch (a) {
throw new Error("something went wrong " + a)
}
}
}
};
17 changes: 17 additions & 0 deletions graphql/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,27 @@ type Hour{
predictedWeather:Weather
predictedValues:String
}
input HourInput{
hour: Int,
currentWeather:WeatherInput
predictedWeather:WeatherInput
predictedValues:String
}
input WeatherInput{
temperature: String
date:String
}
type Query {
weathers: [Weather]
hour:[Hour]
modifiedHours:[Hour]
}
type Mutation {
insertHour( hour: HourInput! ): Hour
}
schema{
query: Query
mutation: Mutation
}
`;
export default makeExecutableSchema({typeDefs: schema, resolvers})
26 changes: 8 additions & 18 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import * as express from "express";
import * as schedule from "node-schedule";
import * as req from "request";
import { GraphqlServer } from "./graphql/index";
import { fillDB, creteHour } from "./repository/index";
import { Hour } from "./repository/models/Hour/model";
import { Weather } from "./repository/models/Weather/model";
import { insertHour } from "./service/HourService";
import { example } from "./datasourceexample";
const app = express();
const PORT = 1000;
const PORTQRAPHQLSERVICE = 2000;
Expand All @@ -26,21 +25,12 @@ app.get('/find-me-on-github/:user', (request, response) => {
`));

});
app.get('/trigger', async(request, response) => {
const newHour = await creteHour(new Hour({
hour: 0,
currentWeather: new Weather({
temperature: 60,
date: 'just sain'
}),
predictedWeather:new Weather({
temperature: 99,
date: 'just sain - 984488'
}),
predictedValues: "ye well ye"
})
)
response.send(newHour)
app.get('/trigger', async (request, response) => {
console.log("trigger shall begun")

const insertedThingy = await insertHour(example);
console.log(insertedThingy)
response.send(insertedThingy)
});


Expand Down
2 changes: 1 addition & 1 deletion repository/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import mongoose = require('mongoose');
mongoose.Promise = global.Promise;

mongoose.connect('mongodb://127.0.0.1:27017/WeaConPi-Weather');
mongoose.connect('mongodb://127.0.0.1:27017/WeaConPi-Weather5');
export { mongoose }
17 changes: 9 additions & 8 deletions repository/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* Created by Farmas on 23.02.2017.
*/
import { Weather } from "./models/models";
import { IHour, Hour } from "./models/Hour/model";
import { Hour, IHour } from "./models/Hour/model";

export const fillDB = () => Weather.create({
temperature: 55,
date: 'what a nice date'
});

export const creteHour = (hour: IHour) => Hour.create(hour);
export const persistHour = (hour: IHour) =>
Hour.findOneAndUpdate({hour: hour.hour}, {
$set: {
currentWeather: hour.currentWeather,
predictedWeather: hour.predictedWeather,
predictedValues: hour.predictedValues,
}
}, {upsert: true, new: true})



32 changes: 29 additions & 3 deletions repository/models/Hour/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import { mongoose } from "../../database";
import { Schema, Model, Document } from "mongoose";
import { IWeather } from "../Weather/model";

export interface IHour extends Document {
export class HourC {
hour: number;
currentWeather: IWeather;
predictedWeather: IWeather;
predictedValues: string;


constructor(hour: number, currentWeather: IWeather, predictedWeather: IWeather, predictedValues: string) {
this.hour = hour;
this.currentWeather = currentWeather;
this.predictedWeather = predictedWeather;
this.predictedValues = predictedValues;
}
}

export interface IHour extends Document, HourC {
}

export interface IHourModel {
updateHourByHour(hourNumber: number, hour: IHour): Promise<{}>
updateCurrentWeather(id: {}, currentWeather: IWeather): Promise<{}>
updatePredictedWeather(id: {}, predictedWeather: IWeather): Promise<{}>
updatePredictedValues(id: {}, values: string): Promise<{}>
Expand All @@ -21,7 +33,9 @@ export interface IHourModel {
const schema = new Schema({
hour: {
type: Number,
required: true
required: true,
index: true,

},
currentWeather: {
type: Schema.Types.Mixed,
Expand All @@ -38,7 +52,19 @@ const schema = new Schema({

});


schema.static("updateHourByHour", (hourNumber: number, hour: IHour) => {
return Hour
.update({
"hour": hourNumber
}, {
"$set": {
"currentWeather": hour.currentWeather,
"predictedWeather": hour.predictedWeather,
"predictedValues": hour.predictedValues,
}
})
.exec();
});
schema.static("updateCurrentWeather", (id: {}, currentWeather: IWeather) => {
return Hour
.update({
Expand Down
81 changes: 63 additions & 18 deletions repository/models/Weather/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,83 @@
*/
import { mongoose } from "../../database";
import { Schema, Model, Document } from "mongoose";
export class WeatherDayDetail {
id: number;
main: string;
description: string;

export interface IWeather extends Document {
constructor(id: number, main: string, description: string) {
this.id = id;
this.main = main;
this.description = description;
}
}
interface IWeatherDayDetail extends Document, WeatherDayDetail {
}

export class WeatherC {
weatherDetail: IWeatherDayDetail[];
temperature: number;
date: string;
pressure: number;
humidity: number;
tempMin: number;
tempMax: number;
windSpeed: number;
windDeg: number;

constructor(weatherDetail?: IWeatherDayDetail[], temperature?: number, pressure?: number, humidity?: number, tempMin?: number, tempMax?: number, windSpeed?: number, windDeg?: number) {
this.weatherDetail = weatherDetail;
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
this.tempMin = tempMin;
this.tempMax = tempMax;
this.windSpeed = windSpeed;
this.windDeg = windDeg;
}

}

export interface IWeather extends Document,WeatherC {

}

export interface IWeatherModel {
updateTemperature(id: {}, temperature: number): Promise<{ nModified: number }>
}

const schema = new Schema({
temperature: {
type: Number,
required: true
},
date: {
pressure: {
type: Number,
required: true
},
humidity: {
type: Number,
required: true
},
tempMin: {
type: Number,
required: true
},
tempMax: {
type: Number,
required: true
},
windSpeed: {
type: Number,
required: true
},
windDeg: {
type: String,
required: true
}
});

schema.static("updateTemperature", (weather: {}, temperature: number) => {

return Weather
.update({
"_id": weather
}, {
"$set": {
"temperature": temperature
}
})
.exec();
},
weatherDetail: {
type: [Schema.Types.Mixed],
required: true
},
});

export type WeatherModel = Model<IWeather> & IWeatherModel & IWeather;
Expand Down
41 changes: 41 additions & 0 deletions service/HourService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Hour, HourC } from "../repository/models/Hour/model";
import { WeatherC, Weather } from "../repository/models/Weather/model";
import { persistHour } from "../repository/index";
/**
* Created by Farmas on 26.02.2017.
*/
export const insertHour = async(weather) => {
let insertedHour = null;
try {
const detailedWeather = [];
weather.weather.forEach(a => detailedWeather.push({
id: a.id,
main: a.main,
description: a.description
}));
const currentWeather = new Weather(
new WeatherC(
detailedWeather,
weather.main.temp,
weather.main.pressure,
weather.main.humidity,
weather.main.temp_min,
weather.main.temp_max,
weather.wind.speed,
weather.wind.deg,
));
const today = new Date();
const hour = new Hour(new HourC(
today.getHours(),
currentWeather,
new Weather(new WeatherC()),
"hello from parse5456"
));
insertedHour = await persistHour(hour)
} catch (e) {
console.log("Ops");
console.log(e);
throw new Error("Error creating hour " + e)
}
return insertedHour;
};

0 comments on commit 5844568

Please sign in to comment.