Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Write a app with following functions, using Go for the backend. A UI asks the user to input a employee name and returns relevant information about him or her. The app in the backend search through Postgresql database that is installed locally on Ubuntu and return following fields: birth data, name and surname, data of entry at the company. Mock up a dataset with 10 entries, create a database in postgresql with name employees and a table with employees data whose name is employees_data.

minimal working project setup for your app.


🗂 Folder Structure

employee-app/
├── main.go
├── index.html
├── seed.sql

📄 main.go

package main

import (
	"database/sql"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"strings"

	_ "github.com/lib/pq"
)

type Employee struct {
	Name      string
	Surname   string
	BirthDate string
	EntryDate string
}

const (
	host     = "localhost"
	port     = 5432
	user     = "your_postgres_user"       // ← change this
	password = "your_postgres_password"   // ← change this
	dbname   = "employees"
)

var tmpl = template.Must(template.ParseFiles("index.html"))

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodPost {
			name := strings.TrimSpace(r.FormValue("name"))
			log.Println("Form submitted with name:", name)

			employee, found := getEmployeeByName(name)
			if found {
				log.Println("Employee found:", employee)
				tmpl.Execute(w, employee)
			} else {
				log.Println("Employee NOT found.")
				tmpl.Execute(w, struct{ NotFound bool }{true})
			}
			return
		}
		tmpl.Execute(w, nil)
	})

	fmt.Println("Server started at http://localhost:8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func getEmployeeByName(name string) (Employee, bool) {
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)

	db, err := sql.Open("postgres", psqlInfo)
	if err != nil {
		log.Println("DB connection failed:", err)
		return Employee{}, false
	}
	defer db.Close()

	var emp Employee
	query := `SELECT name, surname, birth_date, entry_date FROM employees_data WHERE LOWER(name) = LOWER($1) LIMIT 1`
	row := db.QueryRow(query, name)

	err = row.Scan(&emp.Name, &emp.Surname, &emp.BirthDate, &emp.EntryDate)
	if err != nil {
		log.Println("Query error:", err)
		return Employee{}, false
	}

	return emp, true
}

🖼 index.html

<!DOCTYPE html>
<html>
<head>
    <title>Employee Lookup</title>
</head>
<body>
    <h1>Search Employee</h1>
    <form method="POST">
        <label for="name">Employee Name:</label>
        <input type="text" name="name" id="name" required>
        <button type="submit">Search</button>
    </form>

    {{if .NotFound}}
        <p style="color:red;">Employee not found.</p>
    {{else if .Name}}
        <h2>Employee Information</h2>
        <p><strong>Name:</strong> {{.Name}} {{.Surname}}</p>
        <p><strong>Birth Date:</strong> {{.BirthDate}}</p>
        <p><strong>Entry Date:</strong> {{.EntryDate}}</p>
    {{end}}
</body>
</html>

🐘 seed.sql (initialize database)

CREATE DATABASE employees;

\c employees

CREATE TABLE employees_data (
    id SERIAL PRIMARY KEY,
    name TEXT,
    surname TEXT,
    birth_date DATE,
    entry_date DATE
);

INSERT INTO employees_data (name, surname, birth_date, entry_date) VALUES
('Alice', 'Johnson', '1985-03-10', '2010-07-15'),
('Bob', 'Smith', '1980-01-22', '2008-04-12'),
('Carol', 'White', '1992-11-05', '2019-03-19'),
('David', 'Brown', '1988-06-01', '2011-11-23'),
('Eve', 'Black', '1990-08-14', '2016-09-07'),
('Frank', 'Miller', '1983-05-17', '2009-01-02'),
('Grace', 'Clark', '1995-02-28', '2022-05-14'),
('Hank', 'Adams', '1982-12-09', '2006-10-19'),
('Ivy', 'Davis', '1991-07-25', '2018-06-20'),
('Jack', 'Wilson', '1987-03-03', '2013-12-30');

Run it with:

psql -U your_user -f seed.sql

✅ To Run the App

go run main.go

Visit http://localhost:8080, search for Alice, Bob, etc.

Let me know if you'd like me to Dockerize it or add surname search too!

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages