From 84b683db11e8b98dd97c509279605c6d03beb241 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 24 Sep 2019 18:54:50 +0200 Subject: [PATCH] Removed frontend part and added graphql implementation --- gulpfile.js | 2 +- package-lock.json | 6 ++++ package.json | 1 + src/lib/dataaccess/index.ts | 14 ++++++++- src/public/graphql/schema.graphql | 7 +---- src/public/javascripts/main.js | 0 src/routes/home.ts | 49 ++++++++++++++++++------------- src/views/home/feed.pug | 13 -------- src/views/home/friends.pug | 1 - src/views/home/index.pug | 9 ------ src/views/includes/head.pug | 1 - src/views/includes/stylebar.pug | 2 -- src/views/login/index.pug | 8 ----- src/views/login/login.pug | 6 ---- src/views/register/index.pug | 8 ----- src/views/register/register.pug | 8 ----- 16 files changed, 51 insertions(+), 84 deletions(-) delete mode 100644 src/public/javascripts/main.js delete mode 100644 src/views/home/feed.pug delete mode 100644 src/views/home/friends.pug delete mode 100644 src/views/home/index.pug delete mode 100644 src/views/includes/head.pug delete mode 100644 src/views/includes/stylebar.pug delete mode 100644 src/views/login/index.pug delete mode 100644 src/views/login/login.pug delete mode 100644 src/views/register/index.pug delete mode 100644 src/views/register/register.pug diff --git a/gulpfile.js b/gulpfile.js index c1abd43..a5853d1 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,7 +3,7 @@ const sass = require('gulp-sass'); const ts = require('gulp-typescript'); const minify = require('gulp-minify'); const del = require('delete'); - +const gulp = require('gulp'); function clearDist(cb) { del('dist/*', cb); diff --git a/package-lock.json b/package-lock.json index 64e04b1..34608d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3348,6 +3348,12 @@ } } }, + "gulp-angular": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/gulp-angular/-/gulp-angular-0.1.2.tgz", + "integrity": "sha1-ljV2ul7qoDZqMf6l7S7AHvC0O3g=", + "dev": true + }, "gulp-minify": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/gulp-minify/-/gulp-minify-3.1.0.tgz", diff --git a/package.json b/package.json index 7dcee4a..1d42af9 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@types/winston": "^2.4.4", "delete": "^1.1.0", "gulp": "^4.0.2", + "gulp-angular": "^0.1.2", "gulp-minify": "^3.1.0", "gulp-sass": "^4.0.2", "gulp-typescript": "^5.0.1", diff --git a/src/lib/dataaccess/index.ts b/src/lib/dataaccess/index.ts index a0e38f5..c0816e6 100644 --- a/src/lib/dataaccess/index.ts +++ b/src/lib/dataaccess/index.ts @@ -95,7 +95,7 @@ namespace dataaccess { * @param authorId * @param type */ - export async function createPost(content: string, authorId: number, type: string) { + export async function createPost(content: string, authorId: number, type?: string) { const result = await queryHelper.first({ text: "INSERT INTO posts (content, author, type) VALUES ($1, $2, $3) RETURNING *", values: [content, authorId, type], @@ -103,6 +103,18 @@ namespace dataaccess { return new Post(result.id, result); } + /** + * Deletes a post + * @param postId + */ + export async function deletePost(postId: number) { + const result = await queryHelper.first({ + text: "DELETE FROM posts WHERE posts.id = $1", + values: [postId], + }); + return true; + } + /** * Enum representing the types of votes that can be performed on a post. */ diff --git a/src/public/graphql/schema.graphql b/src/public/graphql/schema.graphql index eba344a..bb369f5 100644 --- a/src/public/graphql/schema.graphql +++ b/src/public/graphql/schema.graphql @@ -91,11 +91,9 @@ type User { "represents a single user post" type Post { - "returns the path to the posts picture if it has one" - picture: String "returns the text of the post" - text: String + content: String "upvotes of the Post" upvotes: Int! @@ -111,9 +109,6 @@ type Post { "returns the type of vote the user performed on the post" userVote: VoteType - - "returns the tags of the post" - tags: [String] } "represents a request of any type" diff --git a/src/public/javascripts/main.js b/src/public/javascripts/main.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/routes/home.ts b/src/routes/home.ts index 1ed5115..2a88931 100644 --- a/src/routes/home.ts +++ b/src/routes/home.ts @@ -1,7 +1,6 @@ import {Router} from "express"; import {GraphQLError} from "graphql"; import * as status from "http-status"; -import {constants} from "http2"; import {Server} from "socket.io"; import dataaccess from "../lib/dataaccess"; import {Post} from "../lib/dataaccess/Post"; @@ -18,7 +17,6 @@ class HomeRoute extends Route { constructor() { super(); this.router = Router(); - this.configure(); } /** @@ -56,7 +54,7 @@ class HomeRoute extends Route { req.session.cookiesAccepted = true; return true; }, - async login(args: any) { + async login(args: {email: string, passwordHash: string}) { if (args.email && args.passwordHash) { const user = await dataaccess.getUserByLogin(args.email, args.passwordHash); if (user && user.id) { @@ -80,7 +78,7 @@ class HomeRoute extends Route { return new GraphQLError("User is not logged in."); } }, - async register(args: any) { + async register(args: {username: string, email: string, passwordHash: string}) { if (args.username && args.email && args.passwordHash) { const user = await dataaccess.registerUser(args.username, args.email, args.passwordHash); if (user) { @@ -95,7 +93,7 @@ class HomeRoute extends Route { return new GraphQLError("No username, email or password given."); } }, - async vote(args: any) { + async vote(args: {postId: number, type: dataaccess.VoteType}) { if (args.postId && args.type) { if (req.session.userId) { return await (new Post(args.postId)).vote(req.session.userId, args.type); @@ -108,23 +106,34 @@ class HomeRoute extends Route { return new GraphQLError("No postId or type given."); } }, + async createPost(args: {content: string}) { + if (args.content) { + if (req.session.userId) { + return await dataaccess.createPost(args.content, req.session.userId); + } else { + res.status(status.UNAUTHORIZED); + return new GraphQLError("Not logged in."); + } + } else { + res.status(status.BAD_REQUEST); + return new GraphQLError("Can't create empty post."); + } + }, + async deletePost(args: {postId: number}) { + if (args.postId) { + const post = new Post(args.postId); + if ((await post.author()).id === req.session.userId) { + return await dataaccess.deletePost(post.id); + } else { + res.status(status.FORBIDDEN); + return new GraphQLError("User is not author of the post."); + } + } else { + return new GraphQLError("No postId given."); + } + }, }; } - - /** - * Configures the route. - */ - private configure() { - this.router.get("/", (req, res) => { - res.render("home"); - }); - this.router.get("/login", (req, res) => { - res.render("login"); - }); - this.router.get("/register", (req, res) => { - res.render("register"); - }); - } } export default HomeRoute; diff --git a/src/views/home/feed.pug b/src/views/home/feed.pug deleted file mode 100644 index e3758e1..0000000 --- a/src/views/home/feed.pug +++ /dev/null @@ -1,13 +0,0 @@ -div#feedcontainer - div.postinput - input(type=text placeholder='Post something') - button.submitbutton Submit - div.feeditem - div.itemhead - span.title Testuser - span.handle - a(href='#') @testuser - span.date 23.09.19 10:07 - p.text - | Example Test text. - | This is a test diff --git a/src/views/home/friends.pug b/src/views/home/friends.pug deleted file mode 100644 index ed36fb7..0000000 --- a/src/views/home/friends.pug +++ /dev/null @@ -1 +0,0 @@ -div#friendscontainer diff --git a/src/views/home/index.pug b/src/views/home/index.pug deleted file mode 100644 index 7da0198..0000000 --- a/src/views/home/index.pug +++ /dev/null @@ -1,9 +0,0 @@ -html - head - title Greenvironment Network - include ../includes/head - body - div#content - include ../includes/stylebar - include feed - include friends diff --git a/src/views/includes/head.pug b/src/views/includes/head.pug deleted file mode 100644 index 9e917e6..0000000 --- a/src/views/includes/head.pug +++ /dev/null @@ -1 +0,0 @@ -link(rel='stylesheet' href='stylesheets/style.css') diff --git a/src/views/includes/stylebar.pug b/src/views/includes/stylebar.pug deleted file mode 100644 index c6c36e3..0000000 --- a/src/views/includes/stylebar.pug +++ /dev/null @@ -1,2 +0,0 @@ -div.stylebar - h1 Greenvironment diff --git a/src/views/login/index.pug b/src/views/login/index.pug deleted file mode 100644 index 81c7585..0000000 --- a/src/views/login/index.pug +++ /dev/null @@ -1,8 +0,0 @@ -html - head - title Greenvironment Network Login - include ../includes/head - body - div#content - include ../includes/stylebar - include login diff --git a/src/views/login/login.pug b/src/views/login/login.pug deleted file mode 100644 index f205c95..0000000 --- a/src/views/login/login.pug +++ /dev/null @@ -1,6 +0,0 @@ -div#input-login - input(type=text placeholder='username') - input(type=text placeholder='password') - button.loginButton Login - a(href="/register" ) - | You arenĀ“t part of greenvironment yet? - create a new account diff --git a/src/views/register/index.pug b/src/views/register/index.pug deleted file mode 100644 index 7ade6a7..0000000 --- a/src/views/register/index.pug +++ /dev/null @@ -1,8 +0,0 @@ -html - head - title Greenvironment Network Register - include ../includes/head - body - div#content - include ../includes/stylebar - include register diff --git a/src/views/register/register.pug b/src/views/register/register.pug deleted file mode 100644 index baaf1e6..0000000 --- a/src/views/register/register.pug +++ /dev/null @@ -1,8 +0,0 @@ -div#input-register - input(type=text placeholder='username') - input(type=text placeholder='email') - input(type=text placeholder='password') - input(type=text placeholder='repeat password') - button.registerButton Register - a(href="/login" ) - | You are already part of greenvironment? - login