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..7279818ba5db828c1e2c2fea5109724f260da350 --- /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"; + } + +} 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..ae368e80b3fab479c54b4eabc8d54eac0f3a6e1a --- /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..98026586b72d3c5c2063699a9d77cb73a8343be5 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Student.java @@ -0,0 +1,12 @@ +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..e6d77e0d11ff3d379a93613d3bb2d784494bdde9 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentService.java @@ -0,0 +1,10 @@ +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..077b3124075846538c5422ba946c7b48f453c68e --- /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; + } +} 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