diff --git a/h4h-team7-web/pages/api/post/[postid].ts b/h4h-team7-web/pages/api/post/[postid].ts new file mode 100644 index 0000000..7bc68c5 --- /dev/null +++ b/h4h-team7-web/pages/api/post/[postid].ts @@ -0,0 +1,29 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import PostRepository from '../../../repository/PostRepository' + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + try { + const body = JSON.parse(req.body) + const { postId } = body + switch (req.method) { + case "DELETE": + PostRepository.deletePost(postId) + res.status(200).send + break; + case "PUT": + PostRepository.editPost(postId, body.post) + res.status(200).send + break; + case "GET": + const post = PostRepository.getPostById(postId) + if (post) res.status(200).json(post) + else res.status(404).json({ message: `Post with id [${postId}] not found` }) + break; + default: + res.status(405).json({ message: `Method type ${req.method} is not available on this URL` }) + break; + } + } catch (e) { + res.status(500).json({ message: e.message }) + } +} diff --git a/h4h-team7-web/pages/api/post/create.ts b/h4h-team7-web/pages/api/post/create.ts new file mode 100644 index 0000000..02c56ca --- /dev/null +++ b/h4h-team7-web/pages/api/post/create.ts @@ -0,0 +1,8 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import PostRepository from '../../../repository/PostRepository' + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const { post } = JSON.parse(req.body) + PostRepository.createPost(post); + res.status(201).end(); +} \ No newline at end of file diff --git a/h4h-team7-web/pages/api/post/index.ts b/h4h-team7-web/pages/api/post/index.ts new file mode 100644 index 0000000..ad8f0b3 --- /dev/null +++ b/h4h-team7-web/pages/api/post/index.ts @@ -0,0 +1,7 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import PostRepository from '../../../repository/PostRepository' + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const posts = PostRepository.getPosts() + res.status(200).json(posts) + } \ No newline at end of file diff --git a/h4h-team7-web/pages/api/post/message.ts b/h4h-team7-web/pages/api/post/message.ts new file mode 100644 index 0000000..fc7198d --- /dev/null +++ b/h4h-team7-web/pages/api/post/message.ts @@ -0,0 +1,4 @@ +export default function handler(req, res) { + const { pid } = req.query + res.end(`Post: ${pid}`) + } \ No newline at end of file diff --git a/h4h-team7-web/pages/api/profile/[profileid].ts b/h4h-team7-web/pages/api/profile/[profileid].ts new file mode 100644 index 0000000..fc7198d --- /dev/null +++ b/h4h-team7-web/pages/api/profile/[profileid].ts @@ -0,0 +1,4 @@ +export default function handler(req, res) { + const { pid } = req.query + res.end(`Post: ${pid}`) + } \ No newline at end of file diff --git a/h4h-team7-web/pages/api/profile/create.ts b/h4h-team7-web/pages/api/profile/create.ts new file mode 100644 index 0000000..fc7198d --- /dev/null +++ b/h4h-team7-web/pages/api/profile/create.ts @@ -0,0 +1,4 @@ +export default function handler(req, res) { + const { pid } = req.query + res.end(`Post: ${pid}`) + } \ No newline at end of file diff --git a/h4h-team7-web/pages/index.tsx b/h4h-team7-web/pages/index.tsx index 574792e..7184272 100644 --- a/h4h-team7-web/pages/index.tsx +++ b/h4h-team7-web/pages/index.tsx @@ -1,11 +1,43 @@ -import Head from 'next/head' -import { BaseLayout } from '../layouts/base-layout' -import styles from '../styles/Home.module.css' +import Head from "next/head"; +import { useEffect } from "react"; +import { BaseLayout } from "../layouts/base-layout"; +import { Post } from "../repository/types"; +import styles from "../styles/Home.module.css"; + +const fakePost: Post = { + id: 1, + title: "Title", + hostId: 1, + description: "Description", + location: { + id: 1, + name: "Clapham Common", + postcode: "SW4 1BB", + }, + date: new Date(), +}; export default function Home() { - return ( - - Test - - ) + /** + * Testing creating posts and retrieving + */ + useEffect(() => { + if (typeof window !== "undefined") { + const { host } = window.location; + fetch(`/api/post/create`, { + method: "POST", + body: JSON.stringify({ + post: fakePost, + }), + }) + .then(console.log) + .then(() => { + fetch(`/api/post`) + .then((res) => res.json()) + .then(console.log); + }); + } + }, []); + + return Test; } diff --git a/h4h-team7-web/repository/LocationRepository.ts b/h4h-team7-web/repository/LocationRepository.ts new file mode 100644 index 0000000..60fdb86 --- /dev/null +++ b/h4h-team7-web/repository/LocationRepository.ts @@ -0,0 +1,35 @@ +import { SuggestedLocation } from "./types"; + +class LocationRepository { + + private locations: SuggestedLocation[] = [ + { + id: 0, + name:"Clapham Common Band Stand", + postcode:"SW4 0AA" + }, + { + id: 1, + name:"Clapham library", + postcode:"SW4 0AA" + }, + { + id: 2, + name:"Brickwood Cafe", + postcode:"SW4 0AA" + } + ]; + + constructor() {} + + public getLocations() { + return this.locations; + } + + public getLocationById(id: number): SuggestedLocation | undefined { + return this.locations.find(loc => loc.id === id); + } + +} + +export default new LocationRepository(); \ No newline at end of file diff --git a/h4h-team7-web/repository/PostRepository.ts b/h4h-team7-web/repository/PostRepository.ts new file mode 100644 index 0000000..ce4b219 --- /dev/null +++ b/h4h-team7-web/repository/PostRepository.ts @@ -0,0 +1,31 @@ +import { Post } from "./types"; + +class PostRepository { + + private posts: Post[] = []; + + constructor() {} + + public getPosts() { + return this.posts; + } + + public getPostById(id: number): Post | undefined { + return this.posts.find(p => p.id === id); + } + + public createPost(post: Post): void { + this.posts = [...this.posts, post]; + } + + public editPost(postId: number, post: Post): void { + this.posts = this.posts.map(p => p.id === post.id && p.id === postId ? post: p) + } + + public deletePost(id: number): void { + this.posts = this.posts.filter(p => p.id !== id) + } + +} + +export default new PostRepository(); \ No newline at end of file diff --git a/h4h-team7-web/repository/ProfileRepository.ts b/h4h-team7-web/repository/ProfileRepository.ts new file mode 100644 index 0000000..cac8367 --- /dev/null +++ b/h4h-team7-web/repository/ProfileRepository.ts @@ -0,0 +1,27 @@ +import { Profile } from "./types"; + +class ProfileRepository { + + private profiles: Profile[] = []; + + constructor() {} + + public getProfiles() { + return this.profiles; + } + + public createProfile(profile: Profile): void { + this.profiles = [...this.profiles, profile] + } + + public editProfile(profile: Profile): void { + this.profiles = this.profiles.map(p => p.id === profile.id ? profile : p); + } + + public getProfileById(id: number): Profile | undefined { + return this.profiles.find(profile => profile.id === id); + } + +} + +export default new ProfileRepository(); \ No newline at end of file diff --git a/h4h-team7-web/repository/types.ts b/h4h-team7-web/repository/types.ts new file mode 100644 index 0000000..5882f70 --- /dev/null +++ b/h4h-team7-web/repository/types.ts @@ -0,0 +1,34 @@ +export type SuggestedLocation = { + id: number; + name: string; + postcode: string; +} + +export type Post = { + id: number; + hostId: number; + title: string; + description: string; + location: SuggestedLocation; + date: Date; +} + +export type Profile = { + id: number; + username: string; + // age: number; + // interests: string[]; + // location: string; + languages: string[]; + profile_image: string; +} + +export type User = { + id: number; + email: string; + password: string; +} | { + id: number; + phoneNumber: number; + password: string; +} \ No newline at end of file