Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alantest #10

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions h4h-team7-web/pages/api/post/[postid].ts
Original file line number Diff line number Diff line change
@@ -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 })
}
}
8 changes: 8 additions & 0 deletions h4h-team7-web/pages/api/post/create.ts
Original file line number Diff line number Diff line change
@@ -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();
}
7 changes: 7 additions & 0 deletions h4h-team7-web/pages/api/post/index.ts
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions h4h-team7-web/pages/api/post/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function handler(req, res) {
const { pid } = req.query
res.end(`Post: ${pid}`)
}
4 changes: 4 additions & 0 deletions h4h-team7-web/pages/api/profile/[profileid].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function handler(req, res) {
const { pid } = req.query
res.end(`Post: ${pid}`)
}
4 changes: 4 additions & 0 deletions h4h-team7-web/pages/api/profile/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function handler(req, res) {
const { pid } = req.query
res.end(`Post: ${pid}`)
}
48 changes: 40 additions & 8 deletions h4h-team7-web/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<BaseLayout>
Test
</BaseLayout>
)
/**
* 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 <BaseLayout>Test</BaseLayout>;
}
35 changes: 35 additions & 0 deletions h4h-team7-web/repository/LocationRepository.ts
Original file line number Diff line number Diff line change
@@ -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();
31 changes: 31 additions & 0 deletions h4h-team7-web/repository/PostRepository.ts
Original file line number Diff line number Diff line change
@@ -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();
27 changes: 27 additions & 0 deletions h4h-team7-web/repository/ProfileRepository.ts
Original file line number Diff line number Diff line change
@@ -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();
34 changes: 34 additions & 0 deletions h4h-team7-web/repository/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}