Skip to content

hw5 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
bffc868
feat: add BotClient in scrapper
cyberpanncake Mar 16, 2024
94053cc
fix: rename class and move some classes in new directory
cyberpanncake Mar 16, 2024
db1b677
feat: add ScrapperClient in bot
cyberpanncake Mar 16, 2024
45c978f
fix: change url type in api dto from String to URI
cyberpanncake Mar 16, 2024
3ff6ff1
Merge branch 'hw3' into hw5
cyberpanncake Mar 17, 2024
0773cc6
fix: restructuring packages
cyberpanncake Mar 17, 2024
e881ed5
feat: add domain dto for scrapper
cyberpanncake Mar 17, 2024
df95c39
feat: add repos and services for dto with tests
cyberpanncake Mar 17, 2024
fcf6f8e
feat: replace todo with services calls in scrapper controllers
cyberpanncake Mar 17, 2024
45cf032
fix: db port and sql queries
cyberpanncake Mar 21, 2024
b810ece
feat: start link source parsers realization
cyberpanncake Mar 21, 2024
c32e393
feat: github and stackoverflow link parsers realizations
cyberpanncake Mar 24, 2024
9f5d0b0
fix: change tests according to source parsers
cyberpanncake Mar 24, 2024
7ad2031
feat: add setting to link parser, allows set http response check
cyberpanncake Mar 24, 2024
f1b16f8
fix: parsers regex
cyberpanncake Mar 24, 2024
75533c3
feat: tasks 3-4 (getting updates from sources and sending them to the…
cyberpanncake Mar 24, 2024
f6a8308
fix: remove config injection in commands, add bean injection instead
cyberpanncake Apr 4, 2024
a939667
fix: add transactional annotations to jdbc services
cyberpanncake Apr 4, 2024
9fee4f6
feat: add BotClient in scrapper
cyberpanncake Mar 16, 2024
cfc3ef7
fix: rename class and move some classes in new directory
cyberpanncake Mar 16, 2024
8a9391e
feat: add ScrapperClient in bot
cyberpanncake Mar 16, 2024
b0ba1a3
fix: change url type in api dto from String to URI
cyberpanncake Mar 16, 2024
3975013
fix: restructuring packages
cyberpanncake Mar 17, 2024
90bb156
feat: add domain dto for scrapper
cyberpanncake Mar 17, 2024
1c5bc45
feat: add repos and services for dto with tests
cyberpanncake Mar 17, 2024
0585d70
feat: replace todo with services calls in scrapper controllers
cyberpanncake Mar 17, 2024
815dc97
fix: db port and sql queries
cyberpanncake Mar 21, 2024
3486b87
feat: start link source parsers realization
cyberpanncake Mar 21, 2024
638feb2
feat: github and stackoverflow link parsers realizations
cyberpanncake Mar 24, 2024
a3fdc9f
fix: change tests according to source parsers
cyberpanncake Mar 24, 2024
3c8d6a9
feat: add setting to link parser, allows set http response check
cyberpanncake Mar 24, 2024
0c3e3d4
fix: parsers regex
cyberpanncake Mar 24, 2024
048e850
feat: tasks 3-4 (getting updates from sources and sending them to the…
cyberpanncake Mar 24, 2024
0719111
fix: remove config injection in commands, add bean injection instead
cyberpanncake Apr 4, 2024
9fc9748
fix: add transactional annotations to jdbc services
cyberpanncake Apr 4, 2024
c2b1965
Merge remote-tracking branch 'origin/hw5' into hw5
cyberpanncake Apr 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
<artifactId>kafka</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>edu.java</groupId>
<artifactId>dto</artifactId>
<version>0.1</version>
</dependency>
</dependencies>

<build>
Expand Down
10 changes: 0 additions & 10 deletions bot/src/main/java/edu/java/bot/api/client/ScrapperClient.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.java.bot.api.controller;

import edu.java.bot.api.dto.ApiErrorResponse;
import edu.java.bot.api.dto.LinkUpdateRequest;
import edu.java.dto.api.bot.ApiErrorResponse;
import edu.java.dto.api.bot.LinkUpdateRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -29,5 +29,4 @@ public class UpdateController {
public ResponseEntity<Void> sendUpdate(@Valid @RequestBody LinkUpdateRequest request) {
return ResponseEntity.ok().build();
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package edu.java.bot.api.exception;

import edu.java.bot.api.dto.ApiErrorResponse;
import edu.java.dto.api.bot.ApiErrorResponse;
import java.util.Arrays;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionApiHandler {
@RestControllerAdvice public class ExceptionApiHandler {

@ExceptionHandler(ChatNotExistException.class)
public ResponseEntity<ApiErrorResponse> chatNotExistException(ChatNotExistException exception) {
Expand All @@ -16,24 +16,19 @@ public ResponseEntity<ApiErrorResponse> chatNotExistException(ChatNotExistExcept
HttpStatus.NOT_FOUND.toString(),
exception.getClass().getName(),
exception.getMessage(),
exception.getStackTrace()
Arrays.stream(exception.getStackTrace()).map(StackTraceElement::toString).toArray(String[]::new)
);
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(error);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ApiErrorResponse> anyException(Exception exception) {
@ExceptionHandler(Exception.class) public ResponseEntity<ApiErrorResponse> anyException(Exception exception) {
ApiErrorResponse error = new ApiErrorResponse(
"Неверные параметры запроса",
HttpStatus.BAD_REQUEST.toString(),
"Произошла непредвиденная ошибка на стороне сервера",
HttpStatus.INTERNAL_SERVER_ERROR.toString(),
exception.getClass().getName(),
exception.getMessage(),
exception.getStackTrace()
Arrays.stream(exception.getStackTrace()).map(StackTraceElement::toString).toArray(String[]::new)
);
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(error);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.java.client;
package edu.java.bot.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NonNull;
Expand Down
15 changes: 15 additions & 0 deletions bot/src/main/java/edu/java/bot/client/ScrapperApiException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.java.bot.client;

import edu.java.dto.api.scrapper.ApiErrorResponse;
import lombok.Getter;

@Getter
public class ScrapperApiException extends Exception {
private final ApiErrorResponse error;

public ScrapperApiException(ApiErrorResponse error) {
super("%s: %s. %s - %s\n%s".formatted(error.code(), error.description(),
error.exceptionName(), error.exceptionMessage(), String.join("\n", error.stacktrace())));
this.error = error;
}
}
87 changes: 87 additions & 0 deletions bot/src/main/java/edu/java/bot/client/ScrapperClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package edu.java.bot.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.java.dto.api.scrapper.AddLinkRequest;
import edu.java.dto.api.scrapper.ApiErrorResponse;
import edu.java.dto.api.scrapper.LinkResponse;
import edu.java.dto.api.scrapper.ListLinksResponse;
import edu.java.dto.api.scrapper.RemoveLinkRequest;
import lombok.NonNull;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.ClientResponse;
import reactor.core.publisher.Mono;

public class ScrapperClient extends AbstractClient {
private static final String CHAT_BASE_URL = "/tg-chat";
private static final String LINK_BASE_URL = "/links";
private static final String ID_PATH = "/{id}";
private static final String CHAT_HEADER = "Tg-Chat-Id";

public ScrapperClient(@NonNull String baseUrl, ObjectMapper mapper) {
super(baseUrl, mapper);
}

public void registerChat(long id) {
client.post()
.uri(uriBuilder -> uriBuilder
.path(CHAT_BASE_URL + ID_PATH)
.build(id))
.retrieve()
.onStatus(HttpStatusCode::isError, this::getException)
.bodyToMono(Void.class)
.block();
}

public void deleteChat(long id) {
client.delete()
.uri(uriBuilder -> uriBuilder
.path(CHAT_BASE_URL + ID_PATH)
.build(id))
.retrieve()
.onStatus(HttpStatusCode::isError, this::getException)
.bodyToMono(Void.class)
.block();
}

public ListLinksResponse getLinks(long id) {
return client.get()
.uri(LINK_BASE_URL)
.header(CHAT_HEADER, String.valueOf(id))
.retrieve()
.onStatus(HttpStatusCode::isError, this::getException)
.bodyToMono(ListLinksResponse.class)
.block();
}

public LinkResponse addLink(long id, AddLinkRequest request) {
return client.post()
.uri(LINK_BASE_URL)
.header(CHAT_HEADER, String.valueOf(id))
.accept(MediaType.APPLICATION_JSON)
.body(Mono.just(request), AddLinkRequest.class)
.retrieve()
.onStatus(HttpStatusCode::isError, this::getException)
.bodyToMono(LinkResponse.class)
.block();
}

public LinkResponse deleteLink(long id, RemoveLinkRequest request) {
return client.method(HttpMethod.DELETE)
.uri(LINK_BASE_URL)
.header(CHAT_HEADER, String.valueOf(id))
.accept(MediaType.APPLICATION_JSON)
.body(Mono.just(request), RemoveLinkRequest.class)
.retrieve()
.onStatus(HttpStatusCode::isError, this::getException)
.bodyToMono(LinkResponse.class)
.block();
}

private Mono<ScrapperApiException> getException(ClientResponse response) {
return response
.bodyToMono(ApiErrorResponse.class)
.map(ScrapperApiException::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.java.bot.service;
package edu.java.bot.client.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand All @@ -15,7 +15,7 @@ public ScrapperService() {
public boolean isUserRegistered(Long userId) {
// TODO: добавить работу с БД через scrapper
log.info("%s. Проверка регистрации пользователя %d".formatted(LocalDateTime.now(), userId));
return false;
return true;
}

public void registerUser(Long userId) {
Expand Down
16 changes: 0 additions & 16 deletions bot/src/main/java/edu/java/bot/command/Command.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.java.bot.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.java.bot.api.client.ScrapperClient;
import edu.java.bot.client.ScrapperClient;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand Down
21 changes: 21 additions & 0 deletions bot/src/main/java/edu/java/bot/configuration/CommandConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package edu.java.bot.configuration;

import edu.java.dto.utils.LinkParser;
import edu.java.dto.utils.sources.parsers.GithubParser;
import edu.java.dto.utils.sources.parsers.SourceParser;
import edu.java.dto.utils.sources.parsers.StackoverflowParser;
import java.util.Set;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommandConfig {

@Bean
public LinkParser linkParser() {
return new LinkParser(SourceParser.buildChain(Set.of(
new GithubParser(),
new StackoverflowParser()
)), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.BotCommand;
import com.pengrad.telegrambot.request.SetMyCommands;
import edu.java.bot.command.Command;
import edu.java.bot.command.components.HelpCommand;
import edu.java.bot.telegram.command.Command;
import edu.java.bot.telegram.command.components.HelpCommand;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package edu.java.bot.controller;
package edu.java.bot.telegram;

import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.TelegramException;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.SendResponse;
import edu.java.bot.command.AbstractCommand;
import edu.java.bot.command.Command;
import edu.java.bot.configuration.ApplicationConfig;
import edu.java.bot.configuration.TelegramBotConfig;
import edu.java.bot.telegram.command.AbstractCommand;
import edu.java.bot.telegram.command.Command;
import java.time.LocalDateTime;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,12 +18,12 @@

@Component
@Slf4j
public class Controller {
public class TgBotController {
private final TelegramBot bot;
private final TelegramBotConfig botConfig;

@Autowired
public Controller(ApplicationConfig config, TelegramBotConfig botConfig) {
public TgBotController(ApplicationConfig config, TelegramBotConfig botConfig) {
this.botConfig = botConfig;
bot = botConfig.telegramBot(config);
bot.setUpdatesListener(this::processUpdates, this::processException);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package edu.java.bot.command;
package edu.java.bot.telegram.command;

import com.pengrad.telegrambot.model.Update;
import edu.java.bot.exception.UnregisteredUserException;
import edu.java.bot.exception.command.CommandException;
import edu.java.bot.exception.command.CommandNotExistException;
import edu.java.bot.exception.command.NotCommandException;
import edu.java.bot.exception.link.LinkException;
import edu.java.bot.exception.parameter.ParameterException;
import edu.java.bot.telegram.exception.UnregisteredUserException;
import edu.java.bot.telegram.exception.command.CommandException;
import edu.java.bot.telegram.exception.command.CommandNotExistException;
import edu.java.bot.telegram.exception.command.NotCommandException;
import edu.java.bot.telegram.exception.parameter.ParameterException;
import edu.java.dto.utils.exception.LinkException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.java.bot.command;
package edu.java.bot.telegram.command;

import edu.java.bot.service.ScrapperService;
import edu.java.bot.client.service.ScrapperService;

public abstract class AbstractServiceCommand extends AbstractCommand {
protected ScrapperService service;
Expand Down
16 changes: 16 additions & 0 deletions bot/src/main/java/edu/java/bot/telegram/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.java.bot.telegram.command;

import com.pengrad.telegrambot.model.Update;
import edu.java.bot.telegram.exception.UnregisteredUserException;
import edu.java.bot.telegram.exception.command.CommandException;
import edu.java.bot.telegram.exception.parameter.ParameterException;
import edu.java.dto.utils.exception.LinkException;

public interface Command {

String getName();

String getDescription();

String execute(Update update) throws UnregisteredUserException, ParameterException, CommandException, LinkException;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package edu.java.bot.command;
package edu.java.bot.telegram.command;

import edu.java.bot.exception.UnregisteredUserException;
import edu.java.bot.exception.parameter.WrongNumberParametersException;
import edu.java.bot.service.ScrapperService;
import edu.java.bot.client.service.ScrapperService;
import edu.java.bot.telegram.exception.UnregisteredUserException;
import edu.java.bot.telegram.exception.parameter.WrongNumberParametersException;

public class CommandUtils {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package edu.java.bot.command.components;
package edu.java.bot.telegram.command.components;

import edu.java.bot.command.AbstractCommand;
import edu.java.bot.command.Command;
import edu.java.bot.command.CommandUtils;
import edu.java.bot.exception.parameter.ParameterException;
import edu.java.bot.telegram.command.AbstractCommand;
import edu.java.bot.telegram.command.Command;
import edu.java.bot.telegram.command.CommandUtils;
import edu.java.bot.telegram.exception.parameter.ParameterException;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.core.annotation.Order;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package edu.java.bot.command.components;
package edu.java.bot.telegram.command.components;

import edu.java.bot.command.AbstractServiceCommand;
import edu.java.bot.command.CommandUtils;
import edu.java.bot.exception.UnregisteredUserException;
import edu.java.bot.exception.parameter.ParameterException;
import edu.java.bot.service.ScrapperService;
import edu.java.bot.client.service.ScrapperService;
import edu.java.bot.telegram.command.AbstractServiceCommand;
import edu.java.bot.telegram.command.CommandUtils;
import edu.java.bot.telegram.exception.UnregisteredUserException;
import edu.java.bot.telegram.exception.parameter.ParameterException;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.core.annotation.Order;
Expand Down
Loading