Skip to content

Spring boot#1

Open
BrovkoRoman wants to merge 6 commits intomainfrom
spring-boot
Open

Spring boot#1
BrovkoRoman wants to merge 6 commits intomainfrom
spring-boot

Conversation

@BrovkoRoman
Copy link
Owner

No description provided.

@miffn3
Copy link

miffn3 commented May 3, 2024

нужно исключить папки, добавь в гитигнор(файл .gitignore https://www.atlassian.com/ru/git/tutorials/saving-changes/gitignore): build и .gradle


import static org.example.service.MainServiceImpl.shortLinksDomain;

@RestController
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

добавь @RequestMapping("/url"), чтобы адрес был не корневой, а семантически отделялся по модели данных, в нашем случае урл



if(longLink == null || longLink.isBlank())
return "Incorrect link";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше выкидывать ошибку, сделай кастомную
в идеале почитай про @ControllerAdvice и подключи обработку, будет очень круто

try {
return mainService.getLongLink(shortLinksDomain + shortCode);
} catch(UnknownShortLinkException e) {
return "There is no corresponding long link";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

аналогично с комментом выше

return resultSet.getString("longLink");

System.out.println("Ошибка: данная короткая ссылка не найдена");
System.exit(0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

так не нужно делать, просто брось ошибку, что не найдено, лучше кастомную(свою, например назвать в стиле NotFoundUrlException, если сделаешь @ControllerAdvice, то можешь там её обрабатывать)

return null;
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже нужно вариант

return resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

аналогично комментам выше, лучше бросать ошибку

return resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и тут

}

public void addPairOfLinks(String longLink, String shortCode) {
String sql = "INSERT INTO links(shortCode, longLink) VALUES ('" + shortCode + "', '" + longLink + "')";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

на каждый +  в памяти создается новая строка, лучше используй String.format("Твой текст и нужно вставить тут %s и тут %s", строка1, строка2);
то есть для подстановки нужно написать строку с плейсхолдерами (%s) и вызвать такую функцию из String

}

public boolean containsLongLink(String longLink) {
String sql = "SELECT * FROM links WHERE longLink = '" + longLink + "'";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

аналогично комменту выше, или можно в обоих случаях использовать PreparedStatement, в нем можно оставлять плейсхолдеры в виде: ? и уже сетать дальше, как в проекте-примере

@miffn3
Copy link

miffn3 commented May 3, 2024

нужно поправить по комментам и снова посмотрю

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments