add experience

This commit is contained in:
Dilanka-H
2025-11-14 15:10:08 +07:00
parent 58cec3e440
commit 5ffde5265b
7 changed files with 134 additions and 14 deletions

View File

@@ -3,34 +3,35 @@ package api
import (
"database/sql"
"duhweb/internal/store"
"fmt"
"html/template"
"net/http"
)
type ProjectHandler struct {
type ApiHandler struct {
Templates *template.Template
ProjectStore *store.SQLiteProjectStore
ExperienceStore *store.SQLiteExperienceStore
}
func (h *ProjectHandler) Render(w http.ResponseWriter, tmpl string, data interface{}) {
func (h *ApiHandler) Render(w http.ResponseWriter, tmpl string, data interface{}) {
if err := h.Templates.ExecuteTemplate(w, tmpl, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func NewProjectHandler(db *sql.DB) *ProjectHandler {
func NewApiHandler(db *sql.DB) *ApiHandler {
tmpl := template.Must(template.New("").ParseGlob("views/*.html"))
template.Must(tmpl.ParseGlob("views/partials/*.html"))
return &ProjectHandler{
return &ApiHandler{
Templates: tmpl,
ProjectStore: store.NewSQLiteProjectStore(db),
ExperienceStore: store.NewSQLiteExperienceStore(db),
}
}
func (h *ProjectHandler) InitPage(w http.ResponseWriter, r *http.Request) {
func (h *ApiHandler) InitPage(w http.ResponseWriter, r *http.Request) {
var hxRequest = r.Header.Get("HX-Request")
if hxRequest != "true" {
h.Render(w, "index", nil)
@@ -39,9 +40,8 @@ func (h *ProjectHandler) InitPage(w http.ResponseWriter, r *http.Request) {
h.Render(w, "about-partial", nil)
}
func (h *ProjectHandler) ProjectsPage(w http.ResponseWriter, r *http.Request) {
func (h *ApiHandler) ProjectsPage(w http.ResponseWriter, r *http.Request) {
var hxRequest = r.Header.Get("HX-Request")
fmt.Println(hxRequest)
projects, err := h.ProjectStore.GetAllProjects()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -53,3 +53,17 @@ func (h *ProjectHandler) ProjectsPage(w http.ResponseWriter, r *http.Request) {
}
h.Render(w, "projects-partial", projects)
}
func (h *ApiHandler) ExperiencePage(w http.ResponseWriter, r *http.Request) {
var hxRequest = r.Header.Get("HX-Request")
experiences, err := h.ExperienceStore.GetAllExperiences()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if hxRequest != "true" {
h.Render(w, "experience", experiences)
return
}
h.Render(w, "experience-partial", experiences)
}

View File

@@ -10,7 +10,7 @@ import (
type Application struct {
Logger *log.Logger
ProjectHandler *api.ProjectHandler
ApiHandler *api.ApiHandler
DB *sql.DB
}
@@ -20,10 +20,10 @@ func NewApplication() (*Application, error) {
if err != nil {
return nil, err
}
ProjectHandler := api.NewProjectHandler(sqlDB)
ApiHandler := api.NewApiHandler(sqlDB)
app := &Application{
Logger: logger,
ProjectHandler: ProjectHandler,
ApiHandler: ApiHandler,
DB: sqlDB,
}
return app, nil

View File

@@ -9,7 +9,8 @@ import (
func SetupRoutes(app *app.Application) *chi.Mux {
r := chi.NewRouter()
r.Get("/", app.ProjectHandler.InitPage)
r.Get("/projects", app.ProjectHandler.ProjectsPage)
r.Get("/", app.ApiHandler.InitPage)
r.Get("/projects", app.ApiHandler.ProjectsPage)
r.Get("/experience", app.ApiHandler.ExperiencePage)
return r
}

View File

@@ -0,0 +1,46 @@
package store
import (
"database/sql"
"strings"
)
type Experience struct {
ID int `json:"id"`
Company string `json:"company"`
EmployeeType string `json:"employee_type"`
Position string `json:"position"`
Tasks []string `json:"tasks"`
Tools string `json:"tools"`
Years int `json:"years"`
}
type SQLiteExperienceStore struct {
db *sql.DB
}
func NewSQLiteExperienceStore(db *sql.DB) *SQLiteExperienceStore {
return &SQLiteExperienceStore{db: db}
}
func (s *SQLiteExperienceStore) GetAllExperiences() ([]Experience, error) {
rows, err := s.db.Query("SELECT id, company, employee_type, position, tasks, tools, years FROM experience")
if err != nil {
return nil, err
}
defer rows.Close()
var experience []Experience
for rows.Next() {
var e Experience
var tasks sql.NullString
if err := rows.Scan(&e.ID, &e.Company, &e.EmployeeType, &e.Position, &tasks, &e.Tools, &e.Years); err != nil {
return nil, err
}
if tasks.Valid {
e.Tasks = strings.Split(tasks.String, ",")
}
experience = append(experience, e)
}
return experience, nil
}

Binary file not shown.

23
views/experience.html Normal file
View File

@@ -0,0 +1,23 @@
{{ block "experience" . }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My CV Website</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/htmx.org@1.9.2"></script>
<link rel="stylesheet" href="/css/index.css" />
</head>
<body class="bg-gray-50 text-gray-900 flex flex-col min-h-screen">
<!-- Header Section -->
{{ template "header" }}
<!-- Projects Section -->
<div id="content" class="flex-grow">{{ template "experience-partial" .}}</div>
<!-- Footer Section -->
{{ template "footer" }}
</body>
{{ template "script" }}
</html>
{{ end}}

View File

@@ -0,0 +1,36 @@
{{ block "experience-partial" . }}
<h2 class="text-3xl font-bold mb-6">Experience</h2>
{{ range . }} {{ template "experience-partial-range" . }} {{ end}} {{ end }} {{
block "experience-partial-range" . }}
<div class="m-10 mb-6 overflow-x-auto rounded-lg shadow">
<table class="min-w-full border border-gray-300 border-collapse">
<thead class="bg-gray-100">
<tr class="border-b border-gray-300">
<th class="px-4 py-3 text-center text-sm font-semibold text-gray-700 border-r border-gray-300">Company</th>
<th class="px-4 py-3 text-center text-sm font-semibold text-gray-700 border-r border-gray-300">Position</th>
<th class="px-4 py-3 text-center text-sm font-semibold text-gray-700 border-r border-gray-300">Employee Type</th>
<th class="px-4 py-3 text-center text-sm font-semibold text-gray-700 border-r border-gray-300">Tasks</th>
<th class="px-4 py-3 text-center text-sm font-semibold text-gray-700 border-r border-gray-300">Tools</th>
<th class="px-4 py-3 text-center text-sm font-semibold text-gray-700">Years</th>
</tr>
</thead>
<tbody>
<tr class="hover:bg-gray-50 border-b border-gray-300">
<td class="px-4 py-3 text-center text-gray-600 border-r border-gray-300">{{ .Company }}</td>
<td class="px-4 py-3 text-center text-gray-600 border-r border-gray-300">{{ .Position }}</td>
<td class="px-4 py-3 text-center text-gray-600 border-r border-gray-300">{{ .EmployeeType }}</td>
<td class="px-4 py-3 text-center text-gray-600 border-r border-gray-300">
<ul class="list-disc list-inside">
{{ range .Tasks }}
<li>{{ . }}</li>
{{ end }}
</ul>
</td>
<td class="px-4 py-3 text-center text-gray-600 border-r border-gray-300">{{ .Tools }}</td>
<td class="px-4 py-3 text-center text-gray-600">{{ .Years }}</td>
</tr>
</tbody>
</table>
</div>
{{ end }}