diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/StudentController.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/StudentController.java new file mode 100644 index 0000000000000000000000000000000000000000..d319256b99bf30c84f77f1c684f3e7f4b1d9bad2 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/StudentController.java @@ -0,0 +1,46 @@ +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.*; + +import java.util.List; + +@Controller +@RequestMapping("/student") +public class StudentController { + + @Autowired + private StudentService service; + + @GetMapping("/list") + public String studentListPage(Model model) { + List 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"; + } + +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/exception/DuplicateStudentNameException.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/exception/DuplicateStudentNameException.java new file mode 100644 index 0000000000000000000000000000000000000000..966811cadb253c74cc7a4b72764e362eb508fa84 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/exception/DuplicateStudentNameException.java @@ -0,0 +1,8 @@ +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)); + } +} diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Student.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Student.java new file mode 100644 index 0000000000000000000000000000000000000000..ae9d3318ead4b0716e95bf2269ed62993f7bccfb --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Student.java @@ -0,0 +1,11 @@ +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; +} diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/StudentRepository.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/StudentRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..8acd8cabf99d89afae8e8172067dc01401fa5b58 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/StudentRepository.java @@ -0,0 +1,23 @@ +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 studentsInMemory = new ArrayList<>(); + + public Student create(Student student) { + studentsInMemory.add(student); + return student; + } + + public Iterator findAll() { + return studentsInMemory.iterator(); + } +} diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentService.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentService.java new file mode 100644 index 0000000000000000000000000000000000000000..425ec2bc6e34873831dcb4142fd5d78f452ba450 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentService.java @@ -0,0 +1,11 @@ +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 findAll(); +} diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..6bbe83bd43dc81f5b5532394160ff659e551a076 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentServiceImpl.java @@ -0,0 +1,53 @@ +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 allStudents = findAll(); + for(Student dbStudent: allStudents) { + if(dbStudent.getName().equals(student.getName())) { + throw new DuplicateStudentNameException(student.getName()); + } + } + + } + + private void generateNPM(Student student) { + StringBuilder stringBuilder = new StringBuilder(); + for (char letter: student.getName().toCharArray()) { + stringBuilder.append(String.valueOf((int)letter)); + } + String npm = stringBuilder.toString(); + student.setNpm(npm); + } + + @Override + public List findAll() { + Iterator studentIterator = studentRepository.findAll(); + List allStudents = new ArrayList<>(); + studentIterator.forEachRemaining(allStudents::add); + return allStudents; + } +} \ No newline at end of file diff --git a/src/main/resources/templates/createStudent.html b/src/main/resources/templates/createStudent.html new file mode 100644 index 0000000000000000000000000000000000000000..f1421995de003aefde579c2b6ac2b33517019a7e --- /dev/null +++ b/src/main/resources/templates/createStudent.html @@ -0,0 +1,27 @@ + + + + + Create new Student + + + +

Create new Student

+ +
+
+ + + Please enter unique name. +
+
+ + +
+ +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/studentList.html b/src/main/resources/templates/studentList.html new file mode 100644 index 0000000000000000000000000000000000000000..84ab6ad54e0980ae984cb2585ad00f446513b60c --- /dev/null +++ b/src/main/resources/templates/studentList.html @@ -0,0 +1,34 @@ + + + + + Student List + + + + +

Student' List

+Create student + + + + + + + + + + + + + + + + + +
NameNPMAddress
+ + + + + \ No newline at end of file