Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 18c362c4 authored by Fauzan Hanandito's avatar Fauzan Hanandito
Browse files

Merge branch 'task-implementation-student' into 'master'

Implement student page

See merge request !1
parents 8b55aaaf adb39951
Branches luqman
No related tags found
1 merge request!1Implement student page
package id.ac.ui.cs.advprog.tutorial0.controller;
import id.ac.ui.cs.advprog.tutorial0.exception.DuplicateStudentnameException;
import id.ac.ui.cs.advprog.tutorial0.model.Student;
import id.ac.ui.cs.advprog.tutorial0.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService service;
@GetMapping("/list")
public String studentListPage(Model model) {
List<Student> allStudents = service.findAll();
model.addAttribute("students", allStudents);
return "studentList";
}
@GetMapping("/create")
public String createStudentPage(Model model) {
Student student = new Student();
model.addAttribute("student", student);
return "createStudent";
}
@PostMapping("/create")
public String createStudentPost(@ModelAttribute Student student, Model model) {
try {
service.create(student);
} catch (DuplicateStudentnameException e) {
model.addAttribute("error", e);
model.addAttribute("student", student);
return "createStudent";
}
return "redirect:list";
}
}
package id.ac.ui.cs.advprog.tutorial0.exception;
public class DuplicateStudentnameException extends RuntimeException {
public DuplicateStudentnameException(String studentName) {
super(String.format("The student name %s is a duplicate!", studentName));
}
}
package id.ac.ui.cs.advprog.tutorial0.model;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class Student {
private String name;
private String npm;
private String address;
}
package id.ac.ui.cs.advprog.tutorial0.repository;
import id.ac.ui.cs.advprog.tutorial0.model.Student;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Repository
public class StudentRepository {
private List<Student> studentsInMemory = new ArrayList<>();
public Student create(Student student) {
studentsInMemory.add(student);
return student;
}
public Iterator<Student> findAll() {
return studentsInMemory.iterator();
}
}
package id.ac.ui.cs.advprog.tutorial0.service;
import id.ac.ui.cs.advprog.tutorial0.model.Student;
import java.util.List;
public interface StudentService {
public Student create(Student student);
public List<Student> findAll();
}
package id.ac.ui.cs.advprog.tutorial0.service;
import id.ac.ui.cs.advprog.tutorial0.exception.DuplicateStudentnameException;
import id.ac.ui.cs.advprog.tutorial0.model.Student;
import id.ac.ui.cs.advprog.tutorial0.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public Student create(Student student) {
validateName(student);
generateNPM(student);
studentRepository.create(student);
return student;
}
private void validateName(Student student) {
List<Student> allStudents = findAll();
for(Student dbStudent: allStudents) {
if(dbStudent.getName().equals(student.getName())) {
throw new DuplicateStudentnameException(student.getName());
}
}
}
private void generateNPM(Student student) {
}
@Override
public List<Student> findAll() {
Iterator<Student> studentIterator = studentRepository.findAll();
List<Student> allStudents = new ArrayList<>();
studentIterator.forEachRemaining(allStudents::add);
return allStudents;
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Create new Student</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<h3>Create new Student</h3>
<div th:if="${error!=null}" class="alert alert-danger" role="alert">
Duplicate student name
</div>
<form th:action="@{/student/create}" th:object="${student}" method="post">
<div class="form-group">
<label for="nameInput">Name</label>
<input th:field="*{name}" type="text" class="form-control" id="nameInput" aria-describedby="nameHelp" placeholder="Enter student' name">
<small id="nameHelp" class="form-text text-muted">Please enter unique name.</small>
</div>
<div class="form-group">
<label for="addressTextarea">Address</label>
<textarea th:field="*{address}" class="form-control" id="addressTextarea" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Student List</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<h2>Student's List</h2>
<a th:href="@{/student/create}" class="btn btn-primary btn-sm mb-3">Create student</a>
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">NPM</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
<tr>
<td th:text="${student.name}"></td>
<td th:text="${student.npm}"></td>
<td th:text="${student.address}"></td>
</tr>
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment