Skip to content

Commit

Permalink
Apply format and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
CristyNel committed Oct 14, 2024
1 parent 49e6829 commit eb25852
Show file tree
Hide file tree
Showing 23 changed files with 575 additions and 359 deletions.
11 changes: 11 additions & 0 deletions api/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/CristyNel/CV_project/tree/main/api/internal/database"
"github.com/CristyNel/CV_project/tree/main/api/mock"
"github.com/CristyNel/CV_project/tree/main/api/routes"
"github.com/gorilla/sessions"
)

func main() {
Expand All @@ -27,15 +28,25 @@ func main() {
log.Fatalf("\033[1;31;1m * * * 🚨 Failed to initialize the database connection.\033[0m")
}

// Initialize the session store
store := sessions.NewCookieStore([]byte("your-secret-key"))

// Use the mock logger
logger := mock.NewMockLogger()
app := &app.App{
DB: Db,
Logger: logger,
Store: store,
}

// Initialize the router
r := routes.InitializeRouter(app)

// Redirect root URL to login page with GET method
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}).Methods("GET")

app.Logger.Printf("\n\033[1;37;1m * * * 🛫 Starting the HTTP server on port: ➮\033[1;94;1m 8080\033[0m")
if err := http.ListenAndServe(":8080", r); err != nil {
app.Logger.Fatalf("\n * Failed to start HTTP server: %s\n", err)
Expand Down
5 changes: 4 additions & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// bcn/github/CV_project/api/go.mod
module github.com/CristyNel/CV_project/tree/main/api

go 1.22
go 1.23

toolchain go1.23.1

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
Expand All @@ -22,6 +24,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/sessions v1.4.0
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ=
github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand Down
15 changes: 8 additions & 7 deletions api/handlers/home.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// * CV_project/api/handlers/home.go
package handlers

import (
Expand All @@ -9,8 +8,9 @@ import (
"github.com/CristyNel/CV_project/tree/main/api/models"
)

// HomeUsers handles the /users route
func HomeUsers(app *app.App, w http.ResponseWriter, r *http.Request) {
app.Logger.Println(" * * * ☎️ Received request for /users")
app.Logger.Println(" * * * ☎️ Received request for /users")

if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
Expand All @@ -21,7 +21,7 @@ func HomeUsers(app *app.App, w http.ResponseWriter, r *http.Request) {
var users []models.User
rows, err := app.DB.Query("SELECT id, firstname, lastname, email FROM users")
if err != nil {
app.Logger.Println("Error querying database: ", err)
app.Logger.Println(" * * * ⛔️ Error querying database: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand All @@ -31,7 +31,7 @@ func HomeUsers(app *app.App, w http.ResponseWriter, r *http.Request) {
var user models.User
err := rows.Scan(&user.ID, &user.Firstname, &user.Lastname, &user.Email)
if err != nil {
app.Logger.Println("Error scanning row: ", err)
app.Logger.Println(" * * * ⛔️ Error scanning row: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand All @@ -41,17 +41,18 @@ func HomeUsers(app *app.App, w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(users)
}

// Home handles the / route
func Home(app *app.App, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}

app.Logger.Println(" * * * ☎️ Received request for /")
app.Logger.Println(" * * * ☎️ Received request for /")

rows, err := app.DB.Query("SELECT id, jobtitle, firstname, lastname, email, phone, address, city, country, postalcode, dateofbirth, nationality, summary, workexperience, education, skills, languages FROM users")
if err != nil {
app.Logger.Println("Error querying database: ", err)
app.Logger.Println(" * * * ⛔️ Error querying database: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand All @@ -63,7 +64,7 @@ func Home(app *app.App, w http.ResponseWriter, r *http.Request) {
var user models.User
err := rows.Scan(&user.ID, &user.Jobtitle, &user.Firstname, &user.Lastname, &user.Email, &user.Phone, &user.Address, &user.City, &user.Country, &user.Postalcode, &user.Dateofbirth, &user.Nationality, &user.Summary, &user.Workexperience, &user.Education, &user.Skills, &user.Languages)
if err != nil {
app.Logger.Println("Error scanning row: ", err)
app.Logger.Println(" * * * ⛔️ Error scanning row: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down
28 changes: 14 additions & 14 deletions api/handlers/templates.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// * handlers/templates.go
package handlers

import (
Expand All @@ -17,19 +16,20 @@ import (
wkhtml "github.com/SebastiaanKlippert/go-wkhtmltopdf"
)

// GenerateTemplate generates a PDF from a template and user data
func GenerateTemplate(app *app.App, w http.ResponseWriter, r *http.Request) {
app.Logger.Println("Received request for /pdf")

query := r.URL.Query()
template_id := query["template"]
user_id := query["user"]
templateID := query["template"]
userID := query["user"]

iduser_int, err := strconv.Atoi(user_id[0])
iduserINT, err := strconv.Atoi(userID[0])
if err != nil {
log.Printf("An error occurred: %v", err)
}

idtemplate_int, err := strconv.Atoi(template_id[0])
idtemplateINT, err := strconv.Atoi(templateID[0])
if err != nil {
log.Printf("An error occurred: %v", err)
}
Expand All @@ -38,27 +38,27 @@ func GenerateTemplate(app *app.App, w http.ResponseWriter, r *http.Request) {
var template utils.Template

// Get the path of the template
row1 := app.DB.QueryRow("SELECT Path FROM template WHERE id = ?", idtemplate_int)
row1 := app.DB.QueryRow("SELECT Path FROM template WHERE id = ?", idtemplateINT)

if err := row1.Scan(&template.Path); err != nil {
if err == sql.ErrNoRows {
app.Logger.Println("Error scanning row: ", err)
app.Logger.Println(" * * * ⛔️ Error scanning row: ", err)
http.NotFound(w, r)
return
}
http.Error(w, fmt.Sprintf("Error fetching user data: %v", err), http.StatusInternalServerError)
http.Error(w, fmt.Sprintf(" * * * ⛔️ Error fetching user data: %v", err), http.StatusInternalServerError)
return
}

row := app.DB.QueryRow("SELECT * FROM users WHERE id = ?", iduser_int)
row := app.DB.QueryRow("SELECT * FROM users WHERE id = ?", iduserINT)

if err := row.Scan(&user.ID, &user.Jobtitle, &user.Firstname, &user.Lastname, &user.Email, &user.Phone, &user.Address, &user.City, &user.Country, &user.Postalcode, &user.Dateofbirth, &user.Nationality, &user.Summary, &user.Workexperience, &user.Education, &user.Skills, &user.Languages); err != nil {
if err == sql.ErrNoRows {
app.Logger.Println("Error scanning row: ", err)
app.Logger.Println(" * * * ⛔️ Error scanning row: ", err)
http.NotFound(w, r)
return
}
http.Error(w, fmt.Sprintf("Error fetching user data: %v", err), http.StatusInternalServerError)
http.Error(w, fmt.Sprintf(" * * * ⛔️ Error fetching user data: %v", err), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -93,7 +93,7 @@ func GenerateTemplate(app *app.App, w http.ResponseWriter, r *http.Request) {
}

// Read
populateHtml, err := os.ReadFile("./bff/templates/view/populate_template.html")
populateHTML, err := os.ReadFile("./bff/templates/view/populate_template.html")
if err != nil {
log.Fatal(err)
}
Expand All @@ -103,7 +103,7 @@ func GenerateTemplate(app *app.App, w http.ResponseWriter, r *http.Request) {
return
}
// Add HTML page
pdfg.AddPage(wkhtml.NewPageReader(bytes.NewReader(populateHtml)))
pdfg.AddPage(wkhtml.NewPageReader(bytes.NewReader(populateHTML)))
// Create the PDF document in memory
err = pdfg.Create()
if err != nil {
Expand All @@ -115,5 +115,5 @@ func GenerateTemplate(app *app.App, w http.ResponseWriter, r *http.Request) {
log.Fatal(err)
}
// Respond with template and user IDs
fmt.Fprintf(w, "%s, %s", template_id[0], user_id[0])
fmt.Fprintf(w, "%s, %s", templateID[0], userID[0])
}
Loading

0 comments on commit eb25852

Please sign in to comment.