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
5 changes: 5 additions & 0 deletions Drafts/dbdemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
11 changes: 11 additions & 0 deletions Drafts/dbdemo/src/main/java/de/telran/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.telran;

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.telran.controller;

import de.telran.model.Student;
import de.telran.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {

StudentService service;

@Autowired
public StudentController(StudentService service) {
this.service = service;
}

@GetMapping
List<Student> getAllStudents() {
return service.getAllStudents();
}

@PostMapping("/api/students")
Student createStudent(@RequestBody Student student) {
return service.createStudent(student);
}

@PutMapping("/api/students")
Student assignStudentToCourse(@RequestBody Student student) {
service.assignStudentToCourse(student);
return service.getStudentById(student.getStudentId());
}

}
20 changes: 20 additions & 0 deletions Drafts/dbdemo/src/main/java/de/telran/model/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.telran.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.lang.Nullable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Student {
private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long studentId;
private String firstName;
private String lastName;
private @Nullable Long courseId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.telran.repository;

import de.telran.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface StudentRepository extends JpaRepository<Student, Long> {

@Transactional
@Modifying
@Query("UPDATE Student s SET s.courseId = :courseId WHERE s.studentId = :studentId")
int assignStudentToCourse(@Param("studentId") long studentId, @Param("courseId") long courseId);
}
31 changes: 31 additions & 0 deletions Drafts/dbdemo/src/main/java/de/telran/service/StudentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.telran.service;

import de.telran.model.Student;
import de.telran.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

@Autowired
StudentRepository repository;

public List<Student> getAllStudents() {
return repository.findAll();
}

public Student createStudent(Student student) {
return repository.save(student);
}

public void assignStudentToCourse(Student student) {
repository.assignStudentToCourse(student.getStudentId(), student.getCourseId());
}

public Student getStudentById(Long id) {
return repository.getOne(id);
}
}