From 7e5ec289de32854896750702031d2b8f25d472cd Mon Sep 17 00:00:00 2001 From: Atakan Temizkan Date: Sun, 1 Sep 2024 23:57:29 +0300 Subject: [PATCH 1/8] finished core criteria --- .env.example | 4 --- src/client/App.jsx | 48 +++++++++++++++++++++--------- src/server/controllers/movie.js | 47 ++++++++++++++++------------- src/server/controllers/user.js | 52 ++++++++++++++++++--------------- 4 files changed, 90 insertions(+), 61 deletions(-) delete mode 100644 .env.example diff --git a/.env.example b/.env.example deleted file mode 100644 index 73eec69b..00000000 --- a/.env.example +++ /dev/null @@ -1,4 +0,0 @@ -DATABASE_URL="YOUR_DATABASE_URL" - -# This env var must be prefixed with `VITE_` in order to work in the client / Vite React app. -VITE_PORT=4000 diff --git a/src/client/App.jsx b/src/client/App.jsx index bae3b635..a9a8350c 100644 --- a/src/client/App.jsx +++ b/src/client/App.jsx @@ -1,19 +1,19 @@ -import { useEffect, useState } from 'react'; -import './App.css'; -import MovieForm from './components/MovieForm'; -import UserForm from './components/UserForm'; +import { useEffect, useState } from "react"; +import "./App.css"; +import MovieForm from "./components/MovieForm"; +import UserForm from "./components/UserForm"; const port = import.meta.env.VITE_PORT; const apiUrl = `http://localhost:${port}`; function App() { const [movies, setMovies] = useState([]); - + const [newMovie, setNewMovie] = useState(false); useEffect(() => { fetch(`${apiUrl}/movie`) - .then(res => res.json()) - .then(res => setMovies(res.data)); - }, []); + .then((res) => res.json()) + .then((res) => setMovies(res.data)); + }, [newMovie]); /** * HINTS! @@ -34,16 +34,38 @@ function App() { * */ const handleRegister = async ({ username, password }) => { - + await fetch(`${apiUrl}/user/register`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ username: username, password: password }), + }); }; const handleLogin = async ({ username, password }) => { - + const response = await fetch(`${apiUrl}/user/login`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ username: username, password: password }), + }); + const token = await response.json(); + localStorage.setItem("token", token.data); }; const handleCreateMovie = async ({ title, description, runtimeMins }) => { - - } + const response = await fetch(`${apiUrl}/movie`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authentication: localStorage.getItem("token"), + }, + body: JSON.stringify({ + title: title, + description: description, + runtimeMins: runtimeMins, + }), + }); + setNewMovie(!newMovie); + }; return (
@@ -58,7 +80,7 @@ function App() {

Movie list