implement webserver

This commit is contained in:
Aleksandr Trushkin
2023-12-17 21:21:13 +03:00
parent f60ebcfb36
commit 1d4e8e10fb
12 changed files with 499 additions and 179 deletions

View File

@ -240,11 +240,13 @@ func (h *syncSravniHandler) fillCaches(ctx context.Context) error {
return nil
}
courses, err := h.svc.Queries.ListCourses.Handle(ctx, query.ListCourse{})
result, err := h.svc.Queries.ListCourses.Handle(ctx, query.ListCourse{})
if err != nil {
return fmt.Errorf("listing courses: %w", err)
}
courses := result.Courses
h.knownExternalIDs = make(map[string]struct{}, len(courses))
xslice.ForEach(courses, func(c domain.Course) {

View File

@ -1,49 +1,30 @@
package http
import (
"html/template"
"log/slog"
"net/http"
"strconv"
"git.loyso.art/frx/kurious/internal/common/errors"
"git.loyso.art/frx/kurious/internal/common/xslice"
"git.loyso.art/frx/kurious/internal/kurious/app"
"git.loyso.art/frx/kurious/internal/kurious/app/query"
"git.loyso.art/frx/kurious/internal/kurious/domain"
"git.loyso.art/frx/kurious/internal/kurious/service"
)
type courseServer struct {
app app.Application
app service.Application
log *slog.Logger
}
type pagination struct {
page int
perPage int
}
func (p pagination) LimitOffset() (limit, offset int) {
if p.page < 0 {
p.page = 0
}
if p.perPage <= 0 {
p.perPage = defaultPerPage
}
return p.perPage, p.page * p.perPage
nextPageToken string
perPage int
}
func parsePaginationFromQuery(r *http.Request) (out pagination, err error) {
query := r.URL.Query()
if query.Has("page") {
out.page, err = strconv.Atoi(query.Get("page"))
if err != nil {
return out, errors.NewValidationError("page", "bad page value")
}
}
out.nextPageToken = query.Get("next")
if query.Has("per_page") {
out.perPage, err = strconv.Atoi(query.Get("per_page"))
@ -77,42 +58,84 @@ type listCoursesParams struct {
learningType string
}
type templateCourse domain.Course
type baseInfo struct {
ID string
Name string
Description string
}
func mapDomainCourseToTemplate(in ...domain.Course) []templateCourse {
return xslice.Map(in, func(v domain.Course) templateCourse {
return templateCourse(v)
type categoryInfo struct {
baseInfo
Subcategories []subcategoryInfo
}
type subcategoryInfo struct {
baseInfo
Courses []domain.Course
}
type listCoursesTemplateParams struct {
Categories []categoryInfo
}
func mapDomainCourseToTemplate(in ...domain.Course) listCoursesTemplateParams {
coursesBySubcategory := make(map[string][]domain.Course, len(in))
subcategoriesByCategories := make(map[string]map[string]struct{}, len(in))
xslice.ForEach(in, func(c domain.Course) {
coursesBySubcategory[c.Thematic] = append(coursesBySubcategory[c.Thematic], c)
if _, ok := subcategoriesByCategories[c.LearningType]; !ok {
subcategoriesByCategories[c.LearningType] = map[string]struct{}{}
}
subcategoriesByCategories[c.LearningType][c.Thematic] = struct{}{}
})
var out listCoursesTemplateParams
for category, subcategoryMap := range subcategoriesByCategories {
outCategory := categoryInfo{}
outCategory.ID = category
outCategory.Name = category
outCategory.Description = ""
for subcategory := range subcategoryMap {
outSubCategory := subcategoryInfo{
Courses: coursesBySubcategory[subcategory],
}
outSubCategory.ID = subcategory
outSubCategory.Name = subcategory
outSubCategory.Description = ""
outCategory.Subcategories = append(outCategory.Subcategories, outSubCategory)
}
}
return out
}
func (c courseServer) List(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
params, err := parseListCoursesParams(r)
if err != nil {
handleError(ctx, err, w, c.log, "unable to parse list courses params")
if handleError(ctx, err, w, c.log, "unable to parse list courses params") {
return
}
limit, offset := params.LimitOffset()
courses, err := c.app.Queries.ListCourses.Handle(ctx, query.ListCourse{
result, err := c.app.Queries.ListCourses.Handle(ctx, query.ListCourse{
CourseThematic: params.courseThematic,
LearningType: params.learningType,
Limit: limit,
Offset: offset,
Limit: params.perPage,
NextPageToken: params.nextPageToken,
})
if err != nil {
handleError(ctx, err, w, c.log, "unable to list courses")
}
templateCourses := mapDomainCourseToTemplate(courses...)
err = template.Must(template.ParseFiles("templates/list.tmpl")).Execute(w, templateCourses)
if err != nil {
handleError(ctx, err, w, c.log, "unable to execute template")
if handleError(ctx, err, w, c.log, "unable to list courses") {
return
}
w.WriteHeader(http.StatusOK)
courses := result.Courses
templateCourses := mapDomainCourseToTemplate(courses...)
err = listTemplateParsed.ExecuteTemplate(w, "courses", templateCourses)
if handleError(ctx, err, w, c.log, "unable to execute template") {
return
}
}

View File

@ -0,0 +1,99 @@
package http
import "html/template"
var listTemplateParsed = template.Must(
template.New("courses").
Parse(listTemplate),
)
const listTemplate = `{{define "courses"}}
<!DOCTYPE html>
<html>
<head>
<title>Courses</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: white;
padding: 10px;
}
header h1 {
margin: 0;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
}
nav li {
display: inline;
margin-right: 10px;
}
nav a {
color: white;
text-decoration: none;
}
h1, h2, h3 {
margin-top: 0;
}
p {
margin-bottom: 10px;
}
.course-plate {
background-color: #f2f2f2;
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
text-align: center;
}
.course-plate a {
color: #333;
text-decoration: none;
}
.course-plate a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<header>
<h1>Courses</h1>
<nav>
<ul>
<li><a href="/">Main page</a></li>
<li><a href="/about">About us</a></li>
<li><a href="/help">Help</a></li>
</ul>
</nav>
</header>
<h1>Courses</h1>
{{range $category := .Categories}}
<h2>{{$category.Name}}</h2>
<p>{{$category.Description}}</p>
{{range $subcategory := .Subcategories}}
<h2>{{$subcategory.Name}}</h2>
<p>{{$subcategory.Description}}</p>
{{range $course := $.Courses}}
<div class="course-plate">
<h3><a href="/courses/{{$course.ID}}">{{$course.Name}}</a></h3>
<p>{{$course.Description}}</p>
<p>Full price: {{$course.FullPrice}}</p>
<p>Discount: {{$course.Discount}}</p>
<p>Thematic: {{$course.Thematic}}</p>
<p>Learning type: {{$course.LearningType}}</p>
<p>Duration: {{$course.Duration}}</p>
<p>Starts at: {{$course.StartsAt}}</p>
</div>
{{end}}
{{end}}
{{end}}
</body>
</html>
{{end}}`

View File

@ -8,30 +8,28 @@ import (
"git.loyso.art/frx/kurious/internal/common/errors"
"git.loyso.art/frx/kurious/internal/common/xcontext"
"git.loyso.art/frx/kurious/internal/kurious/app"
)
const (
defaultPerPage = 50
"git.loyso.art/frx/kurious/internal/kurious/service"
)
type Server struct {
app app.Application
app service.Application
log *slog.Logger
}
func NewServer(app app.Application) Server {
return Server{}
}
func (s Server) Courses() courseServer {
return courseServer{
app: s.app,
func NewServer(app service.Application, log *slog.Logger) Server {
return Server{
app: app,
log: log,
}
}
func handleError(ctx context.Context, err error, w http.ResponseWriter, log *slog.Logger, msg string) {
func (s Server) Courses() courseServer {
return courseServer(s)
}
func handleError(ctx context.Context, err error, w http.ResponseWriter, log *slog.Logger, msg string) bool {
if err == nil {
return
return false
}
var errorString string
@ -52,4 +50,6 @@ func handleError(ctx context.Context, err error, w http.ResponseWriter, log *slo
xcontext.LogWithWarnError(ctx, log, err, msg, slog.Int("status_code", code), slog.String("response", errorString))
http.Error(w, errorString, code)
return true
}