Skip to content
Snippets Groups Projects
Commit b5d026fb authored by Jonáš Jelínek's avatar Jonáš Jelínek
Browse files

new imports, routing of get schedule for student id from second MS

parent 9fed7589
No related branches found
No related tags found
No related merge requests found
......@@ -24,13 +24,9 @@ public abstract class Account extends AbstractEntity {
@Basic
@Column(name = "last_name", nullable = false, length = 20)
protected String lastName;
// @OneToOne(mappedBy = "owner")
// @JoinTable(name = "schedule", joinColumns = @JoinColumn(name = "id"))
// protected Schedule schedule;
@Basic
@Column(name = "scheduleId", nullable = true, length = 20)
protected Integer scheduleId;
@Column(name = "schedule", nullable = true, length = 20)
protected Integer schedule;
protected Boolean online;
protected Role role;
......@@ -47,7 +43,7 @@ public abstract class Account extends AbstractEntity {
// return this.schedule;
// }
public Integer getSchedule() {
return this.scheduleId;
return this.schedule;
}
public void setOnline(boolean online) {
......
......@@ -72,13 +72,10 @@ public StudentAccountController(StudentAccountService studentAccountService,
}
}
// @PreAuthorize("hasAnyRole('ROLE_TEACHER', 'ROLE_STUDENT')")
//// todo replace with call to schedule service with account id
// @GetMapping(value = "/{id}/schedule", produces = MediaType.APPLICATION_JSON_VALUE)
// public Schedule getScheduleByUserId(@PathVariable Integer id) {
// return studentAccountService.getScheduleById(id);
// }
@GetMapping(value = "/{id}/schedule", produces = MediaType.APPLICATION_JSON_VALUE)
public Object getScheduleByUserId(@PathVariable Integer id) {
return studentAccountService.getScheduleById(id);
}
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Object[]> getStudents() {
......@@ -138,18 +135,17 @@ public StudentAccountController(StudentAccountService studentAccountService,
}
//@PreAuthorize("hasAnyRole('ROLE_TEACHER', 'ROLE_STUDENT')")
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/{id}/join/class/{classId}")
public ResponseEntity<Void> joinClass(@PathVariable Integer id, @PathVariable Integer classId) {
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/{studentId}/join/class/{classId}")
public ResponseEntity<Void> joinClass(@PathVariable Integer studentId, @PathVariable Integer classId) {
// SClass sclass = null;
StudentAccount user = null;
try{
//todo
//fetch class from 2nd service, via classId
//fetch course id
//fetch course from id
int courseId = 0;
int sclassId = 0;
// sclass = classService.find(classId);
user = studentAccountService.findStudent(id);
user = studentAccountService.findStudent(studentId);
Course course = courseService.find(courseId);
//check if user has joined the corresponding course
......@@ -173,7 +169,8 @@ public StudentAccountController(StudentAccountService studentAccountService,
classesFromSchedule.add(sclassId);
//todo replace with call to schedule service adding classes to schedule
// tempSchedule.setClasses(classesFromSchedule);
studentAccountService.addToSchedule(classId, studentId);
// scheduleService.update(tempSchedule);
studentAccountService.update(user);
......
......@@ -7,6 +7,8 @@ import ear.kos2.dao.StudentAccountDao;
import ear.kos2.exception.AccountException;
import ear.kos2.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -85,19 +87,41 @@ public class StudentAccountService extends AccountService{
}
//todo replace with call to schedule service with account id
// @Transactional
// public List getScheduleById(int idAccount){
// int scheduleId = studentAccountDao.find(idAccount).getSchedule();
// String url = "http://localhost:8082/ClassService/schedule/" + scheduleId;
// try {
// List temp = restTemplate.getForObject(url, List.class);
// if(temp != null) {
// return temp;
// } else {
// return Collections.emptyList();
// }
// } catch (RestClientException e) {
// return Collections.emptyList();
// }
// }
@Transactional
public List getScheduleById(int idAccount){
public Object getScheduleById(int idAccount) {
int scheduleId = studentAccountDao.find(idAccount).getSchedule();
String url = "http://localhost:8082/ClassService/schedule/" + scheduleId;
try {
return restTemplate.getForObject(url, List.class);
ResponseEntity<Object> response = restTemplate.exchange(
"http://localhost:8082/ClassService/schedule/{id}",
HttpMethod.GET,
null,
Object.class,
scheduleId
);
return response.getBody();
} catch (RestClientException e) {
e.printStackTrace();
return Collections.emptyList();
// Handle exception if needed
return null;
}
}
@Transactional
public List<Object[]> findAllObjects() {
return studentAccountDao.findAllObjects();
......
......@@ -2,4 +2,17 @@ INSERT INTO course (name, credits, capacity) VALUES ('course 1', 10, 100);
INSERT INTO course (name, credits, capacity) VALUES ('course 2', 20, 200);
INSERT INTO course (name, credits, capacity) VALUES ('course 3', 30, 220);
INSERT INTO account (username, password, first_name, last_name) VALUES ('sample username', 'sample password', 'John', 'Deer');
--INSERT INTO student (program, credits) VALUES ("test program", 10);
\ No newline at end of file
-- Insert into the account table
INSERT INTO account (username, password, first_name, last_name, schedule)
VALUES ('john_doe', 'password123', 'John', 'Doe', 1);
-- Use a temporary table to store the last inserted ID
CREATE TABLE temp_id AS SELECT MAX(id) AS id FROM account;
-- Insert into the student table using the last inserted id from the account table
INSERT INTO student (id_account, program, credits)
SELECT id, 'Computer Science', 30 FROM temp_id;
-- Clean up the temporary table
DROP TABLE temp_id;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment