Compare commits
50 Commits
main
...
zaglooshka
| Author | SHA1 | Date | |
|---|---|---|---|
| 0584a39989 | |||
| f05ea61703 | |||
| aff58ed540 | |||
| dd284cfdd1 | |||
| 46f1af6120 | |||
| e575a33e0e | |||
| 34e6504871 | |||
| 5c88b9f534 | |||
| 56dca69f93 | |||
| 6c182d4e7c | |||
| 4b19a73fd7 | |||
| d92e327d60 | |||
| 058b7e45fa | |||
| 32ea0c3966 | |||
| 356dc79556 | |||
| 93acd2eff9 | |||
| e7ba782ad2 | |||
| 387551bd7b | |||
| 0bd4a82fa2 | |||
| f7f66d2253 | |||
| 015eda4264 | |||
| 5c4840389d | |||
| ffdcbf9cf2 | |||
| b58f7035f1 | |||
| d71fae29e7 | |||
| 50861b6caf | |||
| 16c1822a0e | |||
| ea1b497c93 | |||
| 5c8ca7874f | |||
| 543f9aa46b | |||
| 84bfb6c650 | |||
| 102f1e6cdd | |||
| 4add0bdebb | |||
| a045440b93 | |||
| 00181d738f | |||
| 488bc06cab | |||
| 46bc2115c1 | |||
| 17710475d3 | |||
| 5061d3bdd8 | |||
| ebf9130372 | |||
| 974458477f | |||
| f143194275 | |||
| 86dbc99f89 | |||
| c2f3531e07 | |||
| 4f146aae62 | |||
| c6fdd57d51 | |||
| 7544c614ab | |||
| 8e98abdda4 | |||
| 62917a9589 | |||
| 40a8fb8d4c |
@ -1,7 +0,0 @@
|
||||
def compare(a: str) -> bool:
|
||||
for i in range(len(a)):
|
||||
if(a[i].isdigit() == False):
|
||||
if a[i] == "<":
|
||||
return int(a[:i]) < int(a[i + 1:])
|
||||
elif (a[i] == ">"):
|
||||
return int(a[:i]) > int(a[i + 1:])
|
||||
@ -1,4 +0,0 @@
|
||||
import pandas as pd
|
||||
|
||||
def count_sum(df: pd.DataFrame) -> pd.DataFrame:
|
||||
return df.groupby('Товар')['Количество'].sum().reset_index().set_index('Товар')
|
||||
@ -1,12 +0,0 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
def get_last_friday(a: str) -> str:
|
||||
date = datetime.strptime(a, "%m/%Y").date()
|
||||
next_month = date.replace(day=28) + timedelta(days=7)
|
||||
last_day = next_month - timedelta(days=next_month.day)
|
||||
|
||||
# Идем назад до пятницы
|
||||
while last_day.weekday() != 4:
|
||||
last_day -= timedelta(days=1)
|
||||
|
||||
return last_day.strftime("%d.%m.%Y")
|
||||
20
pom.xml
Normal file
20
pom.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.18</version> <!-- Стабильная версия для Java 8 -->
|
||||
</parent>
|
||||
|
||||
<groupId>org.lanit</groupId>
|
||||
<artifactId>json-stub</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
11
src/main/java/org/lanit/Application.java
Normal file
11
src/main/java/org/lanit/Application.java
Normal file
@ -0,0 +1,11 @@
|
||||
package org.lanit;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
93
src/main/java/org/lanit/controllers/JSONController.java
Normal file
93
src/main/java/org/lanit/controllers/JSONController.java
Normal file
@ -0,0 +1,93 @@
|
||||
package org.lanit.controllers;
|
||||
|
||||
import org.lanit.models.*;
|
||||
import org.lanit.service.JsonProcessingService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
|
||||
@Controller
|
||||
public class JSONController {
|
||||
|
||||
private final JsonProcessingService processingService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JSONController(JsonProcessingService processingService, ObjectMapper objectMapper) {
|
||||
this.processingService = processingService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
|
||||
//Метод для создания JSON ответов с чистым Content-Type
|
||||
|
||||
private ResponseEntity<Object> createJsonResponse(Object body, HttpStatus status) {
|
||||
return ResponseEntity.status(status)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Основной endpoint для обработки JSON запросов
|
||||
* @ResponseBody указывает, что возвращаемое значение должно быть записано в тело ответа
|
||||
*/
|
||||
@PostMapping(value = "/json", produces = "application/json", consumes = "application/json")
|
||||
@ResponseBody
|
||||
public ResponseEntity<?> handleJsonRequest(
|
||||
@RequestParam("action") String action,
|
||||
@RequestBody String jsonBody) throws IOException {
|
||||
|
||||
try {
|
||||
// Обработка действий ADD и DELETE
|
||||
if ("add".equals(action)) {
|
||||
AddRequest addRequest = objectMapper.readValue(jsonBody, AddRequest.class);
|
||||
Info modifiedInfo = processingService.processAdd(addRequest);
|
||||
|
||||
Response response = new Response();
|
||||
response.setInfo(modifiedInfo);
|
||||
response.setUuid(addRequest.getUuid()); // Сохраняем исходный UUID
|
||||
response.setLastUpdate(Instant.now().toString()); // Обновляем время ответа
|
||||
return createJsonResponse(response, HttpStatus.OK);
|
||||
|
||||
} else if ("delete".equals(action)) {
|
||||
DeleteRequest deleteRequest = objectMapper.readValue(jsonBody, DeleteRequest.class);
|
||||
Info modifiedInfo = processingService.processDelete(deleteRequest);
|
||||
|
||||
Response response = new Response();
|
||||
response.setInfo(modifiedInfo);
|
||||
response.setUuid(deleteRequest.getUuid());
|
||||
response.setLastUpdate(Instant.now().toString());
|
||||
return createJsonResponse(response, HttpStatus.OK);
|
||||
|
||||
} else {
|
||||
// Обработка неизвестных действий
|
||||
ErrorResponse error = new ErrorResponse();
|
||||
error.setStatus("error");
|
||||
error.setMessage("Передан некорректный action - " + action);
|
||||
return createJsonResponse(error, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
ErrorResponse error = new ErrorResponse();
|
||||
error.setStatus("error");
|
||||
error.setMessage(e.getMessage());
|
||||
// Определяем HTTP статус на основе текста ошибки
|
||||
HttpStatus status = e.getMessage().contains("тикер") || e.getMessage().contains("индекс")
|
||||
? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST;
|
||||
return createJsonResponse(error, status);
|
||||
|
||||
} catch (IOException e) {
|
||||
// Обработка ошибок парсинга JSON
|
||||
ErrorResponse error = new ErrorResponse();
|
||||
error.setStatus("error");
|
||||
error.setMessage("Ошибка разбора JSON");
|
||||
return createJsonResponse(error, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/main/java/org/lanit/models/Add.java
Normal file
21
src/main/java/org/lanit/models/Add.java
Normal file
@ -0,0 +1,21 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Add {
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
|
||||
@JsonProperty("timeFrame")
|
||||
private int timeFrame;
|
||||
|
||||
@JsonProperty("percent")
|
||||
private int percent;
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public int getTimeFrame() { return timeFrame; }
|
||||
public void setTimeFrame(int timeFrame) { this.timeFrame = timeFrame; }
|
||||
public int getPercent() { return percent; }
|
||||
public void setPercent(int percent) { this.percent = percent; }
|
||||
}
|
||||
26
src/main/java/org/lanit/models/AddRequest.java
Normal file
26
src/main/java/org/lanit/models/AddRequest.java
Normal file
@ -0,0 +1,26 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class AddRequest {
|
||||
@JsonProperty("info")
|
||||
private Info info;
|
||||
|
||||
@JsonProperty("add")
|
||||
private Add add;
|
||||
|
||||
@JsonProperty("uuid")
|
||||
private String uuid;
|
||||
|
||||
@JsonProperty("lastUpdate")
|
||||
private String lastUpdate;
|
||||
|
||||
public Info getInfo() { return info; }
|
||||
public void setInfo(Info info) { this.info = info; }
|
||||
public Add getAdd() { return add; }
|
||||
public void setAdd(Add add) { this.add = add; }
|
||||
public String getUuid() { return uuid; }
|
||||
public void setUuid(String uuid) { this.uuid = uuid; }
|
||||
public String getLastUpdate() { return lastUpdate; }
|
||||
public void setLastUpdate(String lastUpdate) { this.lastUpdate = lastUpdate; }
|
||||
}
|
||||
16
src/main/java/org/lanit/models/Alert.java
Normal file
16
src/main/java/org/lanit/models/Alert.java
Normal file
@ -0,0 +1,16 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Alert {
|
||||
@JsonProperty("timeFrame")
|
||||
private int timeFrame;
|
||||
|
||||
@JsonProperty("percent")
|
||||
private int percent;
|
||||
|
||||
public int getTimeFrame() { return timeFrame; }
|
||||
public void setTimeFrame(int timeFrame) { this.timeFrame = timeFrame; }
|
||||
public int getPercent() { return percent; }
|
||||
public void setPercent(int percent) { this.percent = percent; }
|
||||
}
|
||||
16
src/main/java/org/lanit/models/Delete.java
Normal file
16
src/main/java/org/lanit/models/Delete.java
Normal file
@ -0,0 +1,16 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Delete {
|
||||
@JsonProperty("tickerName")
|
||||
private String tickerName;
|
||||
|
||||
@JsonProperty("alertIndex")
|
||||
private int alertIndex;
|
||||
|
||||
public String getTickerName() { return tickerName; }
|
||||
public void setTickerName(String tickerName) { this.tickerName = tickerName; }
|
||||
public int getAlertIndex() { return alertIndex; }
|
||||
public void setAlertIndex(int alertIndex) { this.alertIndex = alertIndex; }
|
||||
}
|
||||
26
src/main/java/org/lanit/models/DeleteRequest.java
Normal file
26
src/main/java/org/lanit/models/DeleteRequest.java
Normal file
@ -0,0 +1,26 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class DeleteRequest {
|
||||
@JsonProperty("info")
|
||||
private Info info;
|
||||
|
||||
@JsonProperty("delete")
|
||||
private Delete delete;
|
||||
|
||||
@JsonProperty("uuid")
|
||||
private String uuid;
|
||||
|
||||
@JsonProperty("lastUpdate")
|
||||
private String lastUpdate;
|
||||
|
||||
public Info getInfo() { return info; }
|
||||
public void setInfo(Info info) { this.info = info; }
|
||||
public Delete getDelete() { return delete; }
|
||||
public void setDelete(Delete delete) { this.delete = delete; }
|
||||
public String getUuid() { return uuid; }
|
||||
public void setUuid(String uuid) { this.uuid = uuid; }
|
||||
public String getLastUpdate() { return lastUpdate; }
|
||||
public void setLastUpdate(String lastUpdate) { this.lastUpdate = lastUpdate; }
|
||||
}
|
||||
16
src/main/java/org/lanit/models/ErrorResponse.java
Normal file
16
src/main/java/org/lanit/models/ErrorResponse.java
Normal file
@ -0,0 +1,16 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class ErrorResponse {
|
||||
@JsonProperty("status")
|
||||
private String status;
|
||||
|
||||
@JsonProperty("message")
|
||||
private String message;
|
||||
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
public String getMessage() { return message; }
|
||||
public void setMessage(String message) { this.message = message; }
|
||||
}
|
||||
17
src/main/java/org/lanit/models/Info.java
Normal file
17
src/main/java/org/lanit/models/Info.java
Normal file
@ -0,0 +1,17 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import java.util.List;
|
||||
|
||||
public class Info {
|
||||
@JsonProperty("userID")
|
||||
private String userID;
|
||||
|
||||
@JsonProperty("tickers")
|
||||
private List<Ticker> tickers;
|
||||
|
||||
public String getUserID() { return userID; }
|
||||
public void setUserID(String userID) { this.userID = userID; }
|
||||
public List<Ticker> getTickers() { return tickers; }
|
||||
public void setTickers(List<Ticker> tickers) { this.tickers = tickers; }
|
||||
}
|
||||
21
src/main/java/org/lanit/models/Response.java
Normal file
21
src/main/java/org/lanit/models/Response.java
Normal file
@ -0,0 +1,21 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Response {
|
||||
@JsonProperty("info")
|
||||
private Info info;
|
||||
|
||||
@JsonProperty("uuid")
|
||||
private String uuid;
|
||||
|
||||
@JsonProperty("lastUpdate")
|
||||
private String lastUpdate;
|
||||
|
||||
public Info getInfo() { return info; }
|
||||
public void setInfo(Info info) { this.info = info; }
|
||||
public String getUuid() { return uuid; }
|
||||
public void setUuid(String uuid) { this.uuid = uuid; }
|
||||
public String getLastUpdate() { return lastUpdate; }
|
||||
public void setLastUpdate(String lastUpdate) { this.lastUpdate = lastUpdate; }
|
||||
}
|
||||
17
src/main/java/org/lanit/models/Ticker.java
Normal file
17
src/main/java/org/lanit/models/Ticker.java
Normal file
@ -0,0 +1,17 @@
|
||||
package org.lanit.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import java.util.List;
|
||||
|
||||
public class Ticker {
|
||||
@JsonProperty("ticker")
|
||||
private String ticker;
|
||||
|
||||
@JsonProperty("alerts")
|
||||
private List<Alert> alerts;
|
||||
|
||||
public String getTicker() { return ticker; }
|
||||
public void setTicker(String ticker) { this.ticker = ticker; }
|
||||
public List<Alert> getAlerts() { return alerts; }
|
||||
public void setAlerts(List<Alert> alerts) { this.alerts = alerts; }
|
||||
}
|
||||
63
src/main/java/org/lanit/service/JsonProcessingService.java
Normal file
63
src/main/java/org/lanit/service/JsonProcessingService.java
Normal file
@ -0,0 +1,63 @@
|
||||
package org.lanit.service;
|
||||
|
||||
import org.lanit.models.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class JsonProcessingService {
|
||||
|
||||
public Info processAdd(AddRequest request) {
|
||||
Info info = request.getInfo();
|
||||
Add add = request.getAdd();
|
||||
List<Ticker> tickers = info.getTickers();
|
||||
|
||||
// Ищем тикер
|
||||
Optional<Ticker> existingTicker = tickers.stream()
|
||||
.filter(t -> t.getTicker().equals(add.getName()))
|
||||
.findFirst();
|
||||
|
||||
Ticker targetTicker;
|
||||
if (existingTicker.isPresent()) {
|
||||
targetTicker = existingTicker.get();
|
||||
} else {
|
||||
// Создаем новый тикер
|
||||
targetTicker = new Ticker();
|
||||
targetTicker.setTicker(add.getName());
|
||||
targetTicker.setAlerts(new java.util.ArrayList<>());
|
||||
tickers.add(targetTicker);
|
||||
}
|
||||
|
||||
// Создаем и добавляем оповещение
|
||||
Alert newAlert = new Alert();
|
||||
newAlert.setTimeFrame(add.getTimeFrame());
|
||||
newAlert.setPercent(add.getPercent());
|
||||
targetTicker.getAlerts().add(newAlert);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
public Info processDelete(DeleteRequest request) {
|
||||
Info info = request.getInfo();
|
||||
Delete delete = request.getDelete();
|
||||
List<Ticker> tickers = info.getTickers();
|
||||
|
||||
// Ищем тикер
|
||||
Ticker targetTicker = tickers.stream()
|
||||
.filter(t -> t.getTicker().equals(delete.getTickerName()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new RuntimeException("Передан некорректный тикер"));
|
||||
|
||||
// Проверяем индекс
|
||||
List<Alert> alerts = targetTicker.getAlerts();
|
||||
int index = delete.getAlertIndex();
|
||||
if (index < 0 || index >= alerts.size()) {
|
||||
throw new RuntimeException("Передан некорректный индекс");
|
||||
}
|
||||
|
||||
// Удаляем оповещение
|
||||
alerts.remove(index);
|
||||
return info;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user