Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand All @@ -59,6 +63,13 @@
<artifactId>telegrambotsextensions</artifactId>
<version>4.6</version>
</dependency>


<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ru.javacourse.sourcecodebot.telegram.ServiceBot;
import ru.javacourse.sourcecodebot.telegram.handlers.HelpHandler;
import ru.javacourse.sourcecodebot.telegram.handlers.InterviewHandler;
import ru.javacourse.sourcecodebot.telegram.handlers.LessonHandler;

import javax.annotation.PostConstruct;
import java.util.HashMap;
Expand Down Expand Up @@ -37,6 +38,8 @@ public ServiceBot createCommonBot() {
Map<String, MessageHandler> handlers = new HashMap<>();
handlers.put("/help", helpHandler());
handlers.put("/java/interview/random", interviewHandler());
handlers.put("/java/lesson/", lessonHandler());


return new ServiceBot(handlers);
}
Expand All @@ -50,5 +53,10 @@ public HelpHandler helpHandler() {
public InterviewHandler interviewHandler() {
return new InterviewHandler();
}

@Bean
public LessonHandler lessonHandler() {
return new LessonHandler();
}
}

16 changes: 16 additions & 0 deletions src/main/java/ru/javacourse/sourcecodebot/model/Lesson.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public class Lesson {
@Column(name = "chapter")
private String chapter;

@Column(name = "chapter_id")
private String chapterId;

@OneToMany(mappedBy = "lesson")
@LazyCollection(LazyCollectionOption.FALSE)
private List<LessonTask> lessonTasks;

@OneToOne
@JoinColumn(name="resource_id")
private Resource resources;

public Integer getLessonId() {
Expand All @@ -41,11 +45,23 @@ public void setChapter(String chapter) {
this.chapter = chapter;
}

public String getChapter_id() { return chapterId; }

public Resource getResources() {
return resources;
}

public void setResources(Resource resources) {
this.resources = resources;
}

public void setChapter_id(String chapter_id) { this.chapterId = chapter_id; }
@Override
public String toString() {
return "Lesson{" +
"lessonId=" + lessonId +
", chapter='" + chapter + '\'' +
", chapter_id='" + chapterId +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ public class LessonTask {
@Column(name = "lt_id")
private Integer ltId;


@ManyToOne
@JoinColumn(name = "lesson_id")
private Lesson lesson;

@Column(name = "title")
private String title;

@Column(name = "link")
private String link;

@Column(name = "description")
private String description;

public String getLink() { return link; }

public void setLink(String link) { this.link = link; }

public Integer getLtId() {
return ltId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
import ru.javacourse.sourcecodebot.model.Lesson;

public interface LessonRepository extends JpaRepository<Lesson, Integer> {

public Lesson findLessonByChapterId(String chapterId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public SendMessage handle(Update update) {
msg.setChatId(update.getMessage().getChatId());
msg.setText(question.getTitle());


return msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.javacourse.sourcecodebot.telegram.handlers;

import org.springframework.beans.factory.annotation.Autowired;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import ru.javacourse.sourcecodebot.model.Lesson;
import ru.javacourse.sourcecodebot.model.Resource;
import ru.javacourse.sourcecodebot.repository.LessonRepository;
import ru.javacourse.sourcecodebot.telegram.MessageHandler;

public class LessonHandler implements MessageHandler {

@Autowired
private LessonRepository repository;

@Override
public SendMessage handle(Update update) {
Lesson lesson = repository.findLessonByChapterId("2.1");
Resource resource = lesson.getResources();

SendMessage msg = new SendMessage();
msg.enableMarkdown(true);
msg.setChatId(update.getMessage().getChatId());

if (resource != null) {
msg.setText(resource.getLink());
} else {
msg.setText("123");
}
return msg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.javacourse.sourcecodebot.model.Lesson;
import ru.javacourse.sourcecodebot.model.Resource;

import java.util.List;

Expand All @@ -19,4 +20,11 @@ public void testFind() {
List<Lesson> lessons = repository.findAll();
Assertions.assertNotEquals(0, lessons.size());
}

@Test
public void testFindByChapter() {
Lesson lesson = repository.findLessonByChapterId("2.1");
Resource resources = lesson.getResources();
Assertions.assertNotNull(lesson);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ResourceRepositoryTest {
private ResourceRepository repository;

@Test
public void testFind() {
public void testFindAll() {
List<Resource> resources = repository.findAll();
Assertions.assertNotEquals(0, resources.size());
}
Expand Down