Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Commit 7de5ab8

Browse files
committed
Application improvement
1 parent 3c78c34 commit 7de5ab8

File tree

14 files changed

+277
-79
lines changed

14 files changed

+277
-79
lines changed

src/main/java/pl/simplemethod/codebin/githubOauth/GithubRestController.java

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class GithubRestController {
2727

2828
@Autowired
2929
private ContainersRepository containersRepository;
30+
3031
/**
3132
* Returns the necessary information about the repository
3233
*
@@ -103,6 +104,30 @@ ResponseEntity getInfoAboutOwner(@CookieValue("token") String token) {
103104
return new ResponseEntity<>(githubClient.getUserInfo(token).toString(), headers, HttpStatus.valueOf(200));
104105
}
105106

107+
108+
/**
109+
* Getting information from the database about a user
110+
*
111+
* @return Json with data
112+
*/
113+
@GetMapping(value = "/local")
114+
public @ResponseBody
115+
ResponseEntity checkUserPremium(@CookieValue("id") String id) {
116+
org.json.JSONObject body = new org.json.JSONObject();
117+
HttpHeaders headers = new HttpHeaders();
118+
headers.setContentType(MediaType.APPLICATION_JSON);
119+
Users users = usersRepository.getFirstById(Integer.valueOf(id));
120+
body.put("user_id", users.getId());
121+
body.put("github_id", users.getGithub());
122+
if (users.getSubscription() == null) {
123+
body.put("subscription_status", "");
124+
} else {
125+
body.put("subscription_status", users.getSubscription());
126+
}
127+
128+
return new ResponseEntity<>(body.toString(), headers, HttpStatus.valueOf(200));
129+
}
130+
106131
/**
107132
* Returns JSON with user information
108133
*
@@ -191,14 +216,11 @@ ResponseEntity getpublicrepos(@CookieValue("token") String token) {
191216
helper.put("created_at", obj1.get("created_at"));
192217

193218
Containers containers = containersRepository.getFirstByName(obj1.get("id").toString());
194-
if(containers!=null)
195-
{
196-
helper.put("container_create", obj1.get("id"));
197-
198-
}
199-
else
200-
{
201-
helper.put("container_create", null);
219+
if (containers != null) {
220+
helper.put("container_create", obj1.get("id"));
221+
222+
} else {
223+
helper.put("container_create", null);
202224
}
203225
result.add(helper);
204226
}
@@ -214,4 +236,63 @@ ResponseEntity getpublicrepos(@CookieValue("token") String token) {
214236

215237
}
216238
}
239+
240+
241+
/**
242+
* Returns information about one repository of a logged user
243+
*
244+
* @param token Token to github authorize
245+
* @param repoId The identifier of the repository
246+
* @return Json object
247+
*/
248+
@GetMapping("/user/repos/{repoId}")
249+
public @ResponseBody
250+
ResponseEntity getPublicRepo(@CookieValue("token") String token, @PathVariable(value = "repoId") String repoId) {
251+
HttpHeaders headers = new HttpHeaders();
252+
headers.setContentType(MediaType.APPLICATION_JSON);
253+
JSONParser parser = new JSONParser();
254+
org.json.JSONObject body = new org.json.JSONObject();
255+
org.json.simple.JSONArray result = new org.json.simple.JSONArray();
256+
org.json.simple.JSONObject helper = new org.json.simple.JSONObject();
257+
Object obj = null;
258+
try {
259+
Users users = usersRepository.getFirstByToken(token);
260+
if (users == null) {
261+
body.put("token", "errr");
262+
return new ResponseEntity<>(body.toString(), headers, HttpStatus.valueOf(404));
263+
} else {
264+
try {
265+
obj = parser.parse(githubClient.getUserRepos(token));
266+
org.json.simple.JSONArray jsonArray = (org.json.simple.JSONArray) obj;
267+
jsonArray.forEach(item -> {
268+
org.json.simple.JSONObject obj1 = (org.json.simple.JSONObject) item;
269+
if (obj1.get("private").toString().equals("false") && obj1.get("id").toString().equals(repoId)) {
270+
helper.put("language", obj1.get("language"));
271+
helper.put("id", obj1.get("id"));
272+
helper.put("license", obj1.get("license"));
273+
helper.put("name", obj1.get("name"));
274+
helper.put("git_url", obj1.get("git_url"));
275+
helper.put("html_url", obj1.get("html_url"));
276+
helper.put("description", obj1.get("description"));
277+
helper.put("created_at", obj1.get("created_at"));
278+
Containers containers = containersRepository.getFirstByName(obj1.get("id").toString());
279+
if (containers != null) {
280+
helper.put("container_create", obj1.get("id"));
281+
} else {
282+
helper.put("container_create", null);
283+
}
284+
285+
}
286+
});
287+
return new ResponseEntity<>(helper.toString(), headers, HttpStatus.valueOf(200));
288+
} catch (NullPointerException | ParseException | org.json.JSONException e) {
289+
body.put("token", "errr");
290+
return new ResponseEntity<>(body.toString(), headers, HttpStatus.valueOf(404));
291+
}
292+
}
293+
} catch (org.json.JSONException e) {
294+
return new ResponseEntity<>(e, headers, HttpStatus.valueOf(404));
295+
296+
}
297+
}
217298
}

src/main/java/pl/simplemethod/codebin/model/Containers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Containers implements Serializable {
1919
@Column(name = "id_docker", unique = true, nullable = false)
2020
private String idDocker;
2121

22-
@OneToOne(cascade = CascadeType.ALL)
22+
@OneToOne()
2323
@JoinColumn(name = "id_images", referencedColumnName = "id")
2424
private Images image;
2525

src/main/java/pl/simplemethod/codebin/model/Images.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,22 @@ public class Images {
2121
@Column(name = "type")
2222
private String type;
2323

24-
public Images(String name, String type) {
24+
@NonNull
25+
@Column(name = "exec")
26+
private String exec;
27+
28+
public Images(String name, String type, String exec) {
2529
this.name = name;
2630
this.type = type;
31+
this.exec = exec;
32+
}
33+
34+
public String getExec() {
35+
return exec;
36+
}
37+
38+
public void setExec(String exec) {
39+
this.exec = exec;
2740
}
2841

2942
public Images() {

src/main/java/pl/simplemethod/codebin/model/Users.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Users implements Serializable {
2525
@Column(name = "role")
2626
private String role;
2727

28-
@OneToMany
28+
@OneToMany(cascade = CascadeType.REMOVE)
2929
private List<Containers> containers = new ArrayList<>();
3030

3131
public Users() {

src/main/java/pl/simplemethod/codebin/repository/ContainersRepository.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313
@Transactional
1414
public interface ContainersRepository extends JpaRepository<Containers, Long> {
1515

16-
Containers removeById(Long id);
16+
List<Containers> removeByIdDocker(String name);
1717

18-
Containers deleteByName(String name);
19-
20-
Containers removeByName(String name);
21-
22-
Containers deleteContainersByName(String name);
18+
List<Containers> removeAllByIdDocker(String name);
2319

2420
Containers getFirstById(Long id);
2521

src/main/java/pl/simplemethod/codebin/repository/UsersRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public interface UsersRepository extends JpaRepository<Users, Long> {
1515

1616
Users getFirstByToken(String token);
1717

18-
18+
Users getFirstBySubscription(String subscription);
1919
}

src/main/java/pl/simplemethod/codebin/srv/SrvRestController.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ResponseEntity userContainerCheck(@RequestParam("dockergithubid") String dockerG
107107
*/
108108
@PostMapping("container/create")
109109
public @ResponseBody
110-
ResponseEntity createContainerForUser(@RequestParam("dockerimage") String dockerImage, @RequestParam("exposedports") Integer exposedPorts, @RequestParam("hostports") Integer hostPorts, @RequestParam("name") String name, @RequestParam("rammemory") Long ramMemory, @RequestParam("diskquota") Long diskQuota, @CookieValue("id") String id) {
110+
ResponseEntity createContainerForUser(@RequestParam("dockerimage") String dockerImage, @RequestParam("exposedports") Integer exposedPorts, @RequestParam("hostports") Integer hostPorts, @RequestParam("name") String name, @RequestParam("rammemory") Long ramMemory, @RequestParam("diskquota") Long diskQuota, @CookieValue("id") String id, @RequestParam("premiumstatus") Integer premiumStatus, @RequestParam("giturl") String gitUrl) {
111111
HttpHeaders headers = new HttpHeaders();
112112
headers.setContentType(MediaType.APPLICATION_JSON);
113113
Images images = imagesRepository.getFirstById(Integer.valueOf(dockerImage));
@@ -118,7 +118,7 @@ ResponseEntity createContainerForUser(@RequestParam("dockerimage") String docker
118118
status = 200;
119119
List<Containers> containers = new ArrayList<>();
120120

121-
Containers newContainer = new Containers(name, response.get("id").toString(), images, exposedPorts, hostPorts, ramMemory, diskQuota, linkClient.encrypt(String.valueOf(hostPorts)), 1, Instant.now().getEpochSecond());
121+
Containers newContainer = new Containers(name, response.get("id").toString(), images, exposedPorts, hostPorts, ramMemory, diskQuota, linkClient.encrypt(String.valueOf(hostPorts)), premiumStatus, Instant.now().getEpochSecond());
122122
containers.add(newContainer);
123123
containers.forEach(containersRepository::save);
124124
try {
@@ -128,6 +128,19 @@ ResponseEntity createContainerForUser(@RequestParam("dockerimage") String docker
128128
} catch (NumberFormatException e) {
129129
return new ResponseEntity<>(e.toString(), headers, HttpStatus.NOT_FOUND);
130130
}
131+
132+
try
133+
{
134+
Thread.sleep(1000);
135+
Containers containers1 = containersRepository.getFirstByName(name);
136+
response.put("exec", srvClient.execContainer(containers1.getIdDocker(), images.getExec(), gitUrl));
137+
}
138+
catch (InterruptedException e)
139+
{
140+
141+
}
142+
143+
131144
} else {
132145
status = Integer.valueOf(response.get("status").toString());
133146
}
@@ -210,7 +223,7 @@ ResponseEntity logsContainer(@PathVariable(value = "ID") String containerId) {
210223
replaceString = replaceString.replace('�', ' ');
211224

212225
org.json.JSONObject result = new org.json.JSONObject();
213-
result.put("logs",replaceString);
226+
result.put("logs", replaceString);
214227
return new ResponseEntity<>(result.toString(), headers, HttpStatus.valueOf(201));
215228
}
216229

@@ -255,10 +268,7 @@ ResponseEntity deleteContainer(@PathVariable(value = "ID") String containerId) {
255268
HttpHeaders headers = new HttpHeaders();
256269
headers.setContentType(MediaType.APPLICATION_JSON);
257270
org.json.JSONObject response = srvClient.deleteContainer(containerId);
258-
259-
// TODO: 02.06.2019 Poprawić usuwanie
260-
261-
271+
containersRepository.removeByIdDocker(containerId);
262272
return new ResponseEntity<>(response.toString(), headers, HttpStatus.valueOf(response.getInt("status")));
263273
}
264274

@@ -276,6 +286,7 @@ ResponseEntity execContainer(@PathVariable(value = "ID") String containerId, @Re
276286
HttpHeaders headers = new HttpHeaders();
277287
headers.setContentType(MediaType.APPLICATION_JSON);
278288
org.json.JSONObject response = srvClient.execContainer(containerId, path, arguments);
289+
279290
return new ResponseEntity<>(response.toString(), headers, HttpStatus.valueOf(response.getInt("status")));
280291
}
281292

0 commit comments

Comments
 (0)