Added feed retrieval

- added the option to get feed items with getPost sorted by TOP or NEW
- fixed docker compose
pull/2/head
Trivernis 5 years ago
parent 3d25186e49
commit acfea36665

@ -13,3 +13,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- default-config file and generation of config file on startup - default-config file and generation of config file on startup
- DTOs - DTOs
- Home Route - Home Route
- database caching
- session management

@ -1,15 +1,11 @@
version: "3" version: "3"
services: services:
greenvironment: greenvironment:
build: build: .
context: . user: "root"
dockerfile: ./Dockerfile
user: "node"
working_dir: /home/node/green working_dir: /home/node/green
environment: environment:
- NODE_ENV=production - NODE_ENV=production
volumes:
- ./:/home/node/green
ports: ports:
- "8080:8080" - "8080:8080"
command: "npm start" command: "npm start"

@ -223,5 +223,8 @@ export function resolver(req: any, res: any): any {
return new GraphQLError("No sender or type given."); return new GraphQLError("No sender or type given.");
} }
}, },
async getPosts({first, offset, sort}: {first: number, offset: number, sort: dataaccess.SortType}) {
return await dataaccess.getPosts(first, offset, sort);
},
}; };
} }

@ -16,6 +16,9 @@ type Query {
"find a user by user name or handle" "find a user by user name or handle"
findUser(first: Int, offset: Int, name: String!, handle: String!): [User] findUser(first: Int, offset: Int, name: String!, handle: String!): [User]
"returns the post filtered by the sort type with pagination."
getPosts(first: Int=20, offset: Int=0, sort: SortType = NEW): [Post]
} }
type Mutation { type Mutation {
@ -234,3 +237,8 @@ enum RequestType {
GROUPINVITE GROUPINVITE
EVENTINVITE EVENTINVITE
} }
enum SortType {
TOP
NEW
}

@ -123,6 +123,44 @@ namespace dataaccess {
} }
} }
/**
* Returns all posts sorted by new or top with pagination.
* @param first
* @param offset
* @param sort
*/
export async function getPosts(first: number, offset: number, sort: SortType) {
if (sort === SortType.NEW) {
const results = await queryHelper.all({
cache: true,
text: "SELECT * FROM posts ORDER BY created_at DESC LIMIT $1 OFFSET $2",
values: [first, offset],
});
const posts = [];
for (const row of results) {
posts.push(new Post(row.id, row));
}
return posts;
} else {
const results = await queryHelper.all({
cache: true,
text: `
SELECT * FROM (
SELECT *,
(SELECT count(*) FROM votes WHERE vote_type = 'UPVOTE' AND item_id = posts.id) AS upvotes ,
(SELECT count(*) FROM votes WHERE vote_type = 'DOWNVOTE' AND item_id = posts.id) AS downvotes
FROM posts) AS a ORDER BY (a.upvotes - a.downvotes) DESC LIMIT $1 OFFSET $2;
`,
values: [first, offset],
});
const posts = [];
for (const row of results) {
posts.push(new Post(row.id, row));
}
return posts;
}
}
/** /**
* Creates a post * Creates a post
* @param content * @param content
@ -253,6 +291,14 @@ namespace dataaccess {
GROUPINVITE = "GROUPINVITE", GROUPINVITE = "GROUPINVITE",
EVENTINVITE = "EVENTINVITE", EVENTINVITE = "EVENTINVITE",
} }
/**
* Enum representing the types of sorting in the feed.
*/
export enum SortType {
TOP = "TOP",
NEW = "NEW",
}
} }
export default dataaccess; export default dataaccess;

Loading…
Cancel
Save