use json files to store data/ get css to work

This commit is contained in:
2025-11-17 03:13:46 +07:00
parent c772904165
commit a4b3f8ce06
9 changed files with 131 additions and 55 deletions

View File

@@ -8,8 +8,8 @@ import (
)
type ApiHandler struct {
Templates *template.Template
ProjectStore *store.SQLiteProjectStore
Templates *template.Template
ProjectStore *store.SQLiteProjectStore
ExperienceStore *store.SQLiteExperienceStore
}
@@ -19,14 +19,14 @@ func (h *ApiHandler) Render(w http.ResponseWriter, tmpl string, data interface{}
return
}
}
func NewApiHandler(db *sql.DB) *ApiHandler {
tmpl := template.Must(template.New("").ParseGlob("views/*.html"))
template.Must(tmpl.ParseGlob("views/partials/*.html"))
return &ApiHandler{
Templates: tmpl,
ProjectStore: store.NewSQLiteProjectStore(db),
Templates: tmpl,
ProjectStore: store.NewSQLiteProjectStore(db),
ExperienceStore: store.NewSQLiteExperienceStore(db),
}
}
@@ -56,7 +56,8 @@ func (h *ApiHandler) ProjectsPage(w http.ResponseWriter, r *http.Request) {
func (h *ApiHandler) ExperiencePage(w http.ResponseWriter, r *http.Request) {
var hxRequest = r.Header.Get("HX-Request")
experiences, err := h.ExperienceStore.GetAllExperiences()
// experiences, err := h.ExperienceStore.GetAllExperiences()
experiences, err := store.GetAllExperiencesJSON()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return

View File

@@ -6,13 +6,13 @@ import (
)
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"`
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 {
@@ -43,4 +43,4 @@ func (s *SQLiteExperienceStore) GetAllExperiences() ([]Experience, error) {
experience = append(experience, e)
}
return experience, nil
}
}

View File

@@ -0,0 +1,41 @@
package store
import (
"encoding/json"
"fmt"
"io"
"os"
)
type ExperienceJSON struct {
ID int `json:"id"`
Company string `json:"company"`
EmployeeType string `json:"employeeType"`
Position string `json:"position"`
Tasks []string `json:"tasks"`
Tools string `json:"tools"`
Years int `json:"years"`
}
func GetAllExperiencesJSON() ([]ExperienceJSON, error) {
jsonFile, err := os.Open("data/experience.json")
if err != nil {
fmt.Println("File not found; Error: ", err.Error())
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
fmt.Println("Unable to read json file; Error: ", err.Error())
}
var experience []ExperienceJSON
err = json.Unmarshal(byteValue, &experience)
if err != nil {
fmt.Println("Unable to unmarshal; Error: ", err.Error())
}
return experience, nil
}