Skip to content

Commit abdb95e

Browse files
committed
user service impl
1 parent d84d43f commit abdb95e

File tree

6 files changed

+144
-35
lines changed

6 files changed

+144
-35
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.rsa.controller;
2+
3+
import com.example.rsa.model.User;
4+
import com.example.rsa.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
@RestController
10+
@RequestMapping("/user")
11+
public class UserController {
12+
13+
@Autowired
14+
private UserService userService;
15+
16+
@PostMapping
17+
public ResponseEntity<String> addUser(@RequestBody User user) {
18+
try {
19+
userService.saveUser(user);
20+
return ResponseEntity.ok("Kullanıcı başarıyla eklendi.");
21+
} catch (Exception e) {
22+
return ResponseEntity.status(500).body("Kullanıcı eklenirken hata: " + e.getMessage());
23+
}
24+
}
25+
26+
@DeleteMapping("/{id}")
27+
public ResponseEntity<String> deleteUser(@PathVariable Integer id) {
28+
try {
29+
userService.deleteUser(id);
30+
return ResponseEntity.ok("Kullanıcı başarıyla silindi.");
31+
} catch (Exception e) {
32+
return ResponseEntity.status(500).body("Kullanıcı silinirken hata: " + e.getMessage());
33+
}
34+
}
35+
}
36+

rsa-backend/src/main/java/com/example/rsa/model/User.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
import lombok.Data;
44

5+
import java.math.BigInteger;
6+
57
@Data
68
public class User {
79

810
private Integer id;
911
private String name;
10-
private String publicKey;
12+
private Boolean publicKey;
13+
private Boolean privateKey;
1114
private Boolean fileUpload;
1215
private Boolean fileRead;
1316
private Boolean fileSend;
1417
private Boolean fileReceive;
18+
private BigInteger eKey;
19+
private BigInteger dKey;
1520
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.example.rsa.service;
22

3+
import org.springframework.stereotype.Service;
4+
5+
@Service
36
public class FileService {
47
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
package com.example.rsa.service;
22

3+
import com.example.rsa.model.User;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@Service
310
public class UserService {
11+
12+
List<User> users = new ArrayList<>();
13+
14+
public void saveUser(User user) {
15+
users.add(user);
16+
System.out.println(users);
17+
}
18+
19+
public void deleteUser(Integer id) {
20+
users.removeIf(user -> user.getId().equals(id));
21+
System.out.println(users);
22+
}
23+
public User getUserById(Integer id) {
24+
return users.stream()
25+
.filter(user -> user.getId().equals(id))
26+
.findFirst()
27+
.orElse(null);
28+
}
429
}
30+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const API_BASE_URL = process.env.REACT_APP_API_BASE_URL;
22

33
const Endpoints = {
4-
URL: `${API_BASE_URL}/`,
4+
USER : `${API_BASE_URL}/user`,
55
};
66

77
export default Endpoints;

rsa-frontend/src/routes/components/ScenarioArea.js

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React, { useState } from 'react';
22
import Switch from "./Switch";
3+
import axios from 'axios';
4+
import endpoints from "../../contants/endpoints";
5+
import Endpoints from "../../contants/endpoints";
36

47
const initialUsers = [
58
{ id: 1, name: 'Alice', publicKey: false, privateKey: false, fileUpload: false, readFiles: false, eKey: "...", dKey:"..." },
@@ -75,12 +78,45 @@ const UserContainer = ({ user, handleUserToggle }) => {
7578
const ScenarioArea = () => {
7679
const [users, setUsers] = useState(initialUsers);
7780
const [activeUsers, setActiveUsers] = useState([]);
81+
const [loading, setLoading] = useState(false);
82+
7883

7984
const handleUserClick = (userId) => {
8085
if (activeUsers.includes(userId)) {
86+
setLoading(true);
8187
setActiveUsers(activeUsers.filter(id => id !== userId));
88+
axios.delete(`${Endpoints.USER}/${userId}`)
89+
.then(response => {
90+
console.log("Kullanıcı başarıyla silindi:", response.data);
91+
})
92+
.catch(error => {
93+
console.error("Kullanıcı silinirken hata oluştu:", error);
94+
});
95+
setLoading(false);
8296
} else {
97+
setLoading(true);
98+
const user = users.find(u => u.id === userId);
8399
setActiveUsers([...activeUsers, userId]);
100+
101+
axios.post(Endpoints.USER, {
102+
id: user.id,
103+
name: user.name,
104+
publicKey: user.publicKey,
105+
privateKey: user.privateKey,
106+
fileUpload: user.fileUpload,
107+
fileRead: user.readFiles,
108+
fileSend: false,
109+
fileReceive: false,
110+
eKey: user.eKey,
111+
dKey: user.dKey
112+
})
113+
.then(response => {
114+
console.log("Kullanıcı başarıyla eklendi:", response.data);
115+
})
116+
.catch(error => {
117+
console.error("Kullanıcı eklenirken hata oluştu:", error);
118+
});
119+
setLoading(false);
84120
}
85121
};
86122

@@ -93,40 +129,43 @@ const ScenarioArea = () => {
93129
};
94130

95131
return (
96-
<div className="scenario-area">
97-
<p className={"title-text"}><span>Senaryo</span> Alanı</p>
98-
<div className="custom-row">
99-
<p className={"italic"} style={{ marginRight: 12 }}>Kullanıcı<br />Seçim</p>
100-
{users.map((user) => (
101-
<div
102-
key={user.id}
103-
className={`user-button ${activeUsers.includes(user.id) ? 'active' : ''}`}
104-
onClick={() => handleUserClick(user.id)}
105-
>
106-
<img src="/icon/user.png" alt="User Icon" className="mini-icon"
107-
style={{
108-
filter: activeUsers.includes(user.id) ? 'brightness(0) invert(1)' : 'none'
109-
}}
110-
/> {user.name}
111-
</div>
112-
))}
132+
<>
133+
{loading && <div className={"loading-overlay"}><div className={"main-spinner"}></div></div>}
134+
<div className="scenario-area">
135+
<p className={"title-text"}><span>Senaryo</span> Alanı</p>
136+
<div className="custom-row">
137+
<p className={"italic"} style={{ marginRight: 12 }}>Kullanıcı<br />Seçim</p>
138+
{users.map((user) => (
139+
<div
140+
key={user.id}
141+
className={`user-button ${activeUsers.includes(user.id) ? 'active' : ''}`}
142+
onClick={() => handleUserClick(user.id)}
143+
>
144+
<img src="/icon/user.png" alt="User Icon" className="mini-icon"
145+
style={{
146+
filter: activeUsers.includes(user.id) ? 'brightness(0) invert(1)' : 'none'
147+
}}
148+
/> {user.name}
149+
</div>
150+
))}
151+
</div>
152+
{
153+
activeUsers
154+
.sort((a, b) => a - b)
155+
.map((id) => {
156+
const user = users.find(user => user.id === id);
157+
return (
158+
<div key={id}>
159+
<UserContainer
160+
user={user}
161+
handleUserToggle={handleUserToggle}
162+
/>
163+
</div>
164+
);
165+
})
166+
}
113167
</div>
114-
{
115-
activeUsers
116-
.sort((a, b) => a - b)
117-
.map((id) => {
118-
const user = users.find(user => user.id === id);
119-
return (
120-
<div key={id}>
121-
<UserContainer
122-
user={user}
123-
handleUserToggle={handleUserToggle}
124-
/>
125-
</div>
126-
);
127-
})
128-
}
129-
</div>
168+
</>
130169
);
131170
};
132171

0 commit comments

Comments
 (0)