diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/CourseController.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/CourseController.java new file mode 100644 index 0000000000000000000000000000000000000000..cac2c003be1cfea51d10db199ba6e2ffa0d2b333 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/CourseController.java @@ -0,0 +1,39 @@ +package id.ac.ui.cs.advprog.tutorial0.controller; + +import id.ac.ui.cs.advprog.tutorial0.model.Course; +import id.ac.ui.cs.advprog.tutorial0.service.CourseService; +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("/course") +public class CourseController { + + @Autowired + private CourseService service; + + @GetMapping("/list") + public String courseListPage(Model model) { + List allCourses = service.findAll(); + model.addAttribute("courses", allCourses); + return "courseList"; + } + + @GetMapping("/create") + public String createCoursePage(Model model) { + Course course = new Course(); + model.addAttribute("course", course); + return "createCourse"; + } + + @PostMapping("/create") + public String createCoursePost(@ModelAttribute Course course, Model model) { + service.create(course); + return "redirect:list"; + } + +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Course.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Course.java new file mode 100644 index 0000000000000000000000000000000000000000..031b0e296d5be2b688b6cd2cd9ca165eff60e5b2 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Course.java @@ -0,0 +1,11 @@ +package id.ac.ui.cs.advprog.tutorial0.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class Course { + private String courseId; + private String courseName; + private Boolean vacancyStatus; +} diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/CourseRepository.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/CourseRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..a5a63c810cadf9e69487a53273f59f73d8222e2c --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/CourseRepository.java @@ -0,0 +1,23 @@ +package id.ac.ui.cs.advprog.tutorial0.repository; + +import id.ac.ui.cs.advprog.tutorial0.model.Course; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +@Repository +public class CourseRepository { + + private List coursesInMemory = new ArrayList<>(); + + public Course create(Course course) { + coursesInMemory.add(course); + return course; + } + + public Iterator findAll() { + return coursesInMemory.iterator(); + } +} diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseService.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseService.java new file mode 100644 index 0000000000000000000000000000000000000000..df6c10d5e8206e1407a10568c7a1a85ca6ebff1d --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseService.java @@ -0,0 +1,11 @@ +package id.ac.ui.cs.advprog.tutorial0.service; + +import id.ac.ui.cs.advprog.tutorial0.model.Course; + +import java.util.List; + +public interface CourseService { + + public Course create(Course course); + public List findAll(); +} diff --git a/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..3464596b7a7d25a206ce3fb4ec0dc5a046cf25de --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseServiceImpl.java @@ -0,0 +1,42 @@ +package id.ac.ui.cs.advprog.tutorial0.service; + +import id.ac.ui.cs.advprog.tutorial0.model.Course; +import id.ac.ui.cs.advprog.tutorial0.repository.CourseRepository; +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 CourseServiceImpl implements CourseService { + + @Autowired + private CourseRepository courseRepository; + + @Override + public Course create(Course course) { + generateId(course); + course.setVacancyStatus(true); + courseRepository.create(course); + return course; + } + + private void generateId(Course course) { + StringBuilder stringBuilder = new StringBuilder(); + for (char letter: course.getCourseName().toCharArray()) { + stringBuilder.append(String.valueOf((int)letter)); + } + String Id = stringBuilder.toString(); + course.setCourseId(Id); + } + + @Override + public List findAll() { + Iterator courseIterator = courseRepository.findAll(); + List allCourses = new ArrayList<>(); + courseIterator.forEachRemaining(allCourses::add); + return allCourses; + } +} \ No newline at end of file diff --git a/src/main/resources/templates/courseList.html b/src/main/resources/templates/courseList.html new file mode 100644 index 0000000000000000000000000000000000000000..e5a20c882ed72b99ae5b7fe969672a42007b017d --- /dev/null +++ b/src/main/resources/templates/courseList.html @@ -0,0 +1,34 @@ + + + + + Course List + + + + +

Course' List

+Create course + + + + + + + + + + + + + + + + + +
IDNameStatus
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/createCourse.html b/src/main/resources/templates/createCourse.html new file mode 100644 index 0000000000000000000000000000000000000000..7e2f7e4a4eda9312d62980428df0b27ecbfe9a95 --- /dev/null +++ b/src/main/resources/templates/createCourse.html @@ -0,0 +1,20 @@ + + + + + Create new Course + + + +

Create new Course

+
+
+ + + Please enter unique name. +
+ +
+ + + \ No newline at end of file