From 62ec7e0fb49835288187828a8f8fe1e9f3eecc43 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Tue, 28 Feb 2023 11:35:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/ipLab/IpLabApplication.java | 3 +- .../com/example/ipLab/WebConfiguration.java | 13 ++++ .../ipLab/controllers/CalcController.java | 39 +++++++++++ frontend/index.html | 25 +++++++ frontend/package.json | 19 +++++ frontend/scripts/calc.js | 70 +++++++++++++++++++ frontend/style.css | 6 ++ 7 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/WebConfiguration.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java create mode 100644 frontend/index.html create mode 100644 frontend/package.json create mode 100644 frontend/scripts/calc.js create mode 100644 frontend/style.css diff --git a/backend/ipLab/src/main/java/com/example/ipLab/IpLabApplication.java b/backend/ipLab/src/main/java/com/example/ipLab/IpLabApplication.java index 03ec120..c872368 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/IpLabApplication.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/IpLabApplication.java @@ -2,6 +2,8 @@ package com.example.ipLab; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class IpLabApplication { @@ -9,5 +11,4 @@ public class IpLabApplication { public static void main(String[] args) { SpringApplication.run(IpLabApplication.class, args); } - } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/WebConfiguration.java b/backend/ipLab/src/main/java/com/example/ipLab/WebConfiguration.java new file mode 100644 index 0000000..7e79574 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/WebConfiguration.java @@ -0,0 +1,13 @@ +package com.example.ipLab; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfiguration implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry){ + registry.addMapping("/**").allowedMethods("*"); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java b/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java new file mode 100644 index 0000000..9c624de --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java @@ -0,0 +1,39 @@ +package com.example.ipLab.controllers; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CalcController { + + @GetMapping("/second") + public int second(@RequestParam(value = "num") int num){ + return num*num; + } + + @GetMapping("/root") + public double root(@RequestParam(value = "num") int num){ + return Math.sqrt(num); + } + + @GetMapping("/fact") + public int fact(@RequestParam(value = "num") int num){ + int res = 1; + for (int i = 2; i <= num; i++) { + res *= i; + } + return res; + } + + @GetMapping("/digit") + public int digit(@RequestParam(value = "num") int num){ + if (num < 0) num *= -1; + int sum = 0; + while (num > 0) { + sum += num % 10; + num /= 10; + } + return sum; + } +} diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 0000000..5e6fdca --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,25 @@ + + + + + + + + + + Calc + + + +
+
+ + + + + +
+ +
+ + \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json new file mode 100644 index 0000000..d1692fe --- /dev/null +++ b/frontend/package.json @@ -0,0 +1,19 @@ +{ + "name": "ip_lab", + "version": "1.0.0", + "description": "My project for IP lab", + "main": "index.html", + "scripts": { + "start": "http-server -p 3000 ./", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Abazov Andrey", + "license": "ISC", + "dependencies": { + "bootstrap": "5.2.1", + "@fortawesome/fontawesome-free": "6.2.0" + }, + "devDependencies": { + "http-server": "14.1.1" + } + } \ No newline at end of file diff --git a/frontend/scripts/calc.js b/frontend/scripts/calc.js new file mode 100644 index 0000000..091f711 --- /dev/null +++ b/frontend/scripts/calc.js @@ -0,0 +1,70 @@ +function calcSecond(){ + var num = document.getElementById("numberInput").value; + fetch("http://127.0.0.1:8080/second?num=" + num) + .then(function(response) { + if (response.status != 200){ + return response.text().then(text => {throw new Error(text)}); + } + return response.text(); + }) + .then((response) => { + document.getElementById("responseField").innerHTML = "Результат: " + response; + }) + .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) +} + +function calcRoot(){ + var num = document.getElementById("numberInput").value; + if (num < 0) { + document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число"; + return; + } + fetch("http://127.0.0.1:8080/root?num=" + num) + .then((response) => { + if (response.status != 200){ + return response.text().then(text => {throw new Error(text)}); + } + return response.text(); + }) + .then((response) => { + console.log(response); + document.getElementById("responseField").innerHTML = "Результат: " + response; + }) + .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) +} + +function calcFact(){ + var num = document.getElementById("numberInput").value; + if (num < 0) { + document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число"; + return; + } + fetch("http://127.0.0.1:8080/fact?num=" + num) + .then((response) => { + if (response.status != 200){ + return response.text().then(text => {throw new Error(text)}); + } + return response.text(); + }) + .then((response) => { + console.log(response); + document.getElementById("responseField").innerHTML = "Результат: " + response; + }) + .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) +} + +function calcDigit(){ + var num = document.getElementById("numberInput").value; + fetch("http://127.0.0.1:8080/digit?num=" + num) + .then((response) => { + if (response.status != 200){ + return response.text().then(text => {throw new Error(text)}); + } + return response.text(); + }) + .then((response) => { + console.log(response); + document.getElementById("responseField").innerHTML = "Результат: " + response; + }) + .catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;}) +} \ No newline at end of file diff --git a/frontend/style.css b/frontend/style.css new file mode 100644 index 0000000..a4a8155 --- /dev/null +++ b/frontend/style.css @@ -0,0 +1,6 @@ +#responseField{ + font-family: Segoe UI; + font-size: 24px; + color: black; + text-decoration: none; +}