Skip to content

Commit d0baa45

Browse files
committed
Added docker and frontend
1 parent 78f26e2 commit d0baa45

14 files changed

Lines changed: 218 additions & 28 deletions

File tree

onlineshop/docker-compose.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ services:
2020
- "8000:8080"
2121
links:
2222
- db
23+
server:
24+
env_file: .env
25+
build:
26+
context: ./source/server/
27+
dockerfile: ./Dockerfile
28+
image: isuct/web-app:server
29+
environment:
30+
- SOME_VAR=my_var
31+
ports:
32+
- 3000:3000
33+
depends_on:
34+
- db
2335

2436
volumes:
2537
sample:
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import React from 'react';
22

33
import './App.css';
4+
import First from './components/first';
5+
import DogList from './components/dogList';
46

57
const App: React.FC = () => (
6-
<div className="App">
7-
<header className="App-header">
8-
<p>
9-
Edit <code>src/App.tsx</code> and save to reload.
10-
</p>
11-
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
12-
Learn React
13-
</a>
14-
</header>
15-
</div>
8+
<>
9+
<h1>Hello World!</h1>
10+
<h2>Hello</h2>
11+
<First />
12+
<DogList />
13+
</>
1614
);
1715

1816
export default App;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react'
2+
import { Dog } from './dogList'
3+
import Button from '@mui/material/Button';
4+
import TextField from '@mui/material/TextField';
5+
6+
7+
interface IProps {
8+
dog: Dog
9+
}
10+
11+
const updateDog = async (dog: Dog) => {
12+
let method = "POST";
13+
let url = "/api/dogs/";
14+
if (dog.id !== 0) {
15+
method = "PUT";
16+
url += `${dog.id}`;
17+
}
18+
return await fetch(url,{
19+
method: method, // *GET, POST, PUT, DELETE, etc.
20+
headers: {
21+
"Content-Type": "application/json",
22+
// 'Content-Type': 'application/x-www-form-urlencoded',
23+
},
24+
body: JSON.stringify(dog), // body data type must match "Content-Type" header
25+
});
26+
}
27+
28+
export default function DogItem(props: IProps) {
29+
const [name, setName] = React.useState<string>(props.dog.name);
30+
31+
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
32+
setName(event.target.value)
33+
}
34+
return (
35+
<div>
36+
<h1>Dog:</h1>
37+
<TextField
38+
id="standard-basic"
39+
label="Name"
40+
variant="standard"
41+
value={name}
42+
onChange={onChange}
43+
/>
44+
<h4>Age: {props.dog.age}</h4>
45+
<Button variant="contained" color="success" onClick={() => updateDog({...props.dog, name: name})}>
46+
Сохранить
47+
</Button>
48+
</div>
49+
)
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react'
2+
import DogItem from './dogItem';
3+
import { create } from 'domain';
4+
import Button from '@mui/material/Button';
5+
6+
export interface Dog {
7+
id: number,
8+
name: string,
9+
age: number
10+
}
11+
12+
const getDogs = async (): Promise<Dog[]> => {
13+
const data = await fetch('/api/dogs')
14+
return await data.json();
15+
// const dogs = [{ name: "Bob", age: 1 }, { name: "Alice", age: 2 }, { name: "Charlie", age: 3 }]
16+
// return new Promise((resolve) => setTimeout(() => resolve(dogs), 5000));
17+
}
18+
19+
20+
export default function DogList() {
21+
const [dogs, setDogs] = React.useState<Dog[]>([]);
22+
23+
const createDog = async () => {
24+
setDogs([...dogs, { id: 0, name: "", age: 0 }]);
25+
}
26+
27+
React.useEffect(() => {
28+
let isMounted = true;
29+
const fetchDogs = async () => {
30+
console.log("fetching");
31+
const dogs = await getDogs();
32+
if (!isMounted) return;
33+
setDogs(dogs);
34+
}
35+
fetchDogs();
36+
return () => {
37+
isMounted = false;
38+
}
39+
}, []);
40+
return (
41+
<div>
42+
<h1>Dog List</h1>
43+
<Button variant="contained" color="success" onClick={createDog}>
44+
Добавить
45+
</Button>
46+
{
47+
dogs.map((dog, index) => <DogItem dog={dog} key={dog.id} />)
48+
}
49+
</div>
50+
)
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
import Button from '@mui/material/Button';
3+
4+
export default function First() {
5+
const [count, setCount] = React.useState(0);
6+
7+
const handleClick = () => {
8+
setCount(count + 1);
9+
console.log(`Clicked ${count}`)
10+
};
11+
return (
12+
<div>
13+
<h2>Click count: {count}</h2>
14+
<Button variant="contained" onClick={handleClick}>Contained</Button>
15+
</div>
16+
)
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/README.md
2+
**/node_modules/
3+
**/test/
4+
.git
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:20.19.1-alpine
2+
WORKDIR /app
3+
RUN corepack enable
4+
COPY ./package.json ./
5+
COPY ./yarn.lock ./
6+
RUN yarn
7+
COPY ./ ./
8+
ENTRYPOINT yarn run start:dev

onlineshop/source/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"start": "nest start",
1515
"start:dev": "nest start --watch",
1616
"start:debug": "nest start --debug --watch",
17-
"start:prod": "node dist/main",
17+
"start:prod": "node dist/src/main.js",
1818
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
1919
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
2020
"test": "jest",

onlineshop/source/server/src/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Dog } from './dogs/entities/dog.entity';
1111
imports: [
1212
TypeOrmModule.forRoot({
1313
type: 'postgres',
14-
host: 'localhost',
14+
host: 'db',
1515
port: 5432,
1616
username: 'postgres',
1717
password: 'postgres',

onlineshop/source/server/src/cats/cats.controller.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Controller, Get, Post, Body, Patch, Param, Delete, NotFoundException } from '@nestjs/common';
1+
import {
2+
Controller,
3+
Get,
4+
Post,
5+
Body,
6+
Patch,
7+
Param,
8+
Delete,
9+
NotFoundException,
10+
} from '@nestjs/common';
211
import { CatsService } from './cats.service';
312
import { CreateCatDto } from './dto/create-cat.dto';
413
import { UpdateCatDto } from './dto/update-cat.dto';
@@ -21,7 +30,7 @@ export class CatsController {
2130
findOne(@Param('id') id: string) {
2231
const cat = this.catsService.findOne(+id);
2332
if (cat) {
24-
return cat
33+
return cat;
2534
}
2635
throw new NotFoundException(`cat with id: ${id} was not found`);
2736
}

0 commit comments

Comments
 (0)