Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
M MARGARETHA STELLA KALYANADUHITA T
Tutorial 0 AdvProg
Commits
e183c5f6
Commit
e183c5f6
authored
Feb 13, 2022
by
M. Margaretha Stella
Browse files
Implement course page
parent
aed6e92d
Changes
8
Hide whitespace changes
Inline
Side-by-side
src/main/java/id/ac/ui/cs/advprog/turorial0/controller/CourseController.java
0 → 100644
View file @
e183c5f6
package
id.ac.ui.cs.advprog.turorial0.controller
;
import
id.ac.ui.cs.advprog.turorial0.exception.DuplicateCourseNameException
;
import
id.ac.ui.cs.advprog.turorial0.model.Course
;
import
id.ac.ui.cs.advprog.turorial0.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
<
Course
>
allCourses
=
service
.
findAll
();
model
.
addAttribute
(
"courses"
,
allCourses
);
return
"coursesList"
;
}
@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
)
{
try
{
service
.
create
(
course
);
}
catch
(
DuplicateCourseNameException
e
)
{
model
.
addAttribute
(
"error"
,
e
);
model
.
addAttribute
(
"course"
,
course
);
return
"createCourse"
;
}
return
"redirect:list"
;
}
}
src/main/java/id/ac/ui/cs/advprog/turorial0/exception/DuplicateCourseNameException.java
0 → 100644
View file @
e183c5f6
package
id.ac.ui.cs.advprog.turorial0.exception
;
public
class
DuplicateCourseNameException
extends
RuntimeException
{
public
DuplicateCourseNameException
(
String
courseName
)
{
super
(
String
.
format
(
"The course name %s is a duplicate!"
,
courseName
));
}
}
\ No newline at end of file
src/main/java/id/ac/ui/cs/advprog/turorial0/model/Course.java
0 → 100644
View file @
e183c5f6
package
id.ac.ui.cs.advprog.turorial0.model
;
import
lombok.Getter
;
import
lombok.Setter
;
@Getter
@Setter
public
class
Course
{
private
String
courseName
;
private
String
courseId
;
private
boolean
vacancyStatus
=
true
;
}
\ No newline at end of file
src/main/java/id/ac/ui/cs/advprog/turorial0/repository/CourseRepository.java
0 → 100644
View file @
e183c5f6
package
id.ac.ui.cs.advprog.turorial0.repository
;
import
id.ac.ui.cs.advprog.turorial0.model.Course
;
import
id.ac.ui.cs.advprog.turorial0.exception.DuplicateCourseNameException
;
import
org.springframework.stereotype.Repository
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.*
;
@Repository
public
class
CourseRepository
{
private
List
<
Course
>
courseList
=
new
ArrayList
<>();
private
Map
<
String
,
Course
>
courseMap
=
new
HashMap
<>();
public
Course
create
(
Course
course
)
{
if
(
courseMap
.
get
(
course
.
getCourseName
())
==
null
)
{
courseMap
.
put
(
course
.
getCourseName
(),
course
);
courseList
.
add
(
course
);
return
course
;
}
throw
new
DuplicateCourseNameException
(
course
.
getCourseName
());
}
public
Iterator
<
Course
>
findAll
()
{
return
courseList
.
iterator
();
}
}
src/main/java/id/ac/ui/cs/advprog/turorial0/service/CourseService.java
0 → 100644
View file @
e183c5f6
package
id.ac.ui.cs.advprog.turorial0.service
;
import
id.ac.ui.cs.advprog.turorial0.model.Course
;
import
java.util.List
;
public
interface
CourseService
{
public
Course
create
(
Course
course
);
public
List
<
Course
>
findAll
();
}
src/main/java/id/ac/ui/cs/advprog/turorial0/service/CourseServiceImpl.java
0 → 100644
View file @
e183c5f6
package
id.ac.ui.cs.advprog.turorial0.service
;
import
id.ac.ui.cs.advprog.turorial0.model.Course
;
import
id.ac.ui.cs.advprog.turorial0.repository.CourseRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.*
;
@Service
public
class
CourseServiceImpl
implements
CourseService
{
@Autowired
private
CourseRepository
courseRepo
;
@Override
public
Course
create
(
Course
course
)
{
courseRepo
.
create
(
course
);
generateCourseId
(
course
);
return
course
;
}
private
void
generateCourseId
(
Course
course
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
for
(
char
letter
:
course
.
getCourseName
().
toCharArray
())
{
if
(
stringBuilder
.
toString
().
length
()
<=
30
)
{
stringBuilder
.
append
(
String
.
valueOf
((
int
)
letter
));
}
}
course
.
setCourseId
(
stringBuilder
.
toString
());
}
@Override
public
List
<
Course
>
findAll
()
{
Iterator
<
Course
>
courseIterator
=
courseRepo
.
findAll
();
List
<
Course
>
courseList
=
new
ArrayList
<>();
courseIterator
.
forEachRemaining
(
courseList:
:
add
);
return
courseList
;
}
}
src/main/resources/templates/courseList.html
0 → 100644
View file @
e183c5f6
<!DOCTYPE html>
<html
lang=
"en"
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Course 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
class=
"p-2"
>
<div
class=
"w-50 mx-auto"
>
<h2>
Course' List
</h2>
<a
th:href=
"@{/course/create}"
class=
"btn btn-primary btn-sm mb-3"
>
Create course
</a>
<table
class=
"table"
>
<thead>
<tr>
<th
scope=
"col"
>
Course ID
</th>
<th
scope=
"col"
>
Course Name
</th>
<th
scope=
"col"
>
Vacancy Status
</th>
</tr>
</thead>
<tbody
th:each=
"course: ${courses}"
>
<tr>
<td
th:text=
"${course.courseId}"
></td>
<td
th:text=
"${course.courseName}"
></td>
<td
th:text=
"${course.vacancyStatus}"
></td>
</tr>
</tbody>
</table>
</div>
<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
src/main/resources/templates/createCourse.html
0 → 100644
View file @
e183c5f6
<!DOCTYPE html>
<html
lang=
"en"
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Create new Course
</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
class=
"p-2"
>
<div
class=
"w-25 mx-auto"
>
<h3>
Create new Course
</h3>
<form
th:action=
"@{/course/create}"
th:object=
"${course}"
method=
"post"
>
<div
class=
"form-group"
>
<label
for=
"nameInput"
>
Course Name
</label>
<input
th:field=
"*{courseName}"
type=
"text"
class=
"form-control"
id=
"nameInput"
aria-describedby=
"nameHelp"
placeholder=
"Enter course' name"
required
/>
<!-- bagian input field tersebut dipasangkan dengan field `name` pada `Student`.-->
<small
id=
"nameHelp"
class=
"form-text text-muted"
>
Please enter unique course.
</small>
</div>
<div
class=
"form-group"
>
<label
for=
"addressTextarea"
>
Vacancy Status
</label>
<textarea
th:field=
"*{vacancyStatus}"
class=
"form-control"
id=
"addressTextarea"
rows=
"3"
></textarea>
</div>
<button
type=
"submit"
class=
"btn btn-primary my-2"
>
Submit
</button>
</form>
</div>
<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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment