Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Basic Profile Page added #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions models/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"html/template"
"net/http"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)

var db *gorm.DB

type UserProfile struct {
gorm.Model
Username string
Email string
Bio string
}

func profileHandler(w http.ResponseWriter, r *http.Request) {
var user UserProfile
db.First(&user)
renderTemplate(w, "profile.html", user)
}

func editHandler(w http.ResponseWriter, r *http.Request) {
var user UserProfile
db.First(&user)
renderTemplate(w, "edit.html", user)
}

func updateHandler(w http.ResponseWriter, r *http.Request) {
var user UserProfile
db.First(&user)

user.Username = r.FormValue("username")
user.Email = r.FormValue("email")
user.Bio = r.FormValue("bio")

db.Save(&user)

http.Redirect(w, r, "/profile", http.StatusSeeOther)
}

func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
t, err := template.ParseFiles("templates/" + tmpl)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

err = t.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func main() {
var err error
db, err = gorm.Open("sqlite3", "test.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
db.AutoMigrate(&UserProfile{})

http.HandleFunc("/profile", profileHandler)
http.HandleFunc("/edit", editHandler)
http.HandleFunc("/update", updateHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
90 changes: 90 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}

.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h1 {
text-align: center;
color: #333;
}

.profile-info {
text-align: center;
}

.profile-info img {
border-radius: 50%;
max-width: 150px;
height: auto;
}

.profile-info p {
font-size: 18px;
color: #333;
margin-bottom: 10px;
}

.edit-form {
max-width: 600px;
margin: 0 auto;
}

.edit-form label {
display: block;
font-size: 16px;
margin-bottom: 8px;
color: #333;
}

.edit-form input,
.edit-form textarea {
width: calc(100% - 16px);
padding: 8px;
font-size: 16px;
margin-bottom: 16px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}

.edit-form input[type="submit"] {
background-color: #007bff;
color: #fff;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
padding: 10px 20px;
}

.edit-form input[type="submit"]:hover {
background-color: #0056b3;
}

.edit-form textarea {
height: 100px;
}

button {
background-color: #dc3545;
color: #fff;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
padding: 10px 20px;
}

button:hover {
background-color: #bd2130;
}
1 change: 1 addition & 0 deletions views/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<li><a href="/grind">Grind</a></li>
<li><a href="/dasboard">Dashboard</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="/profile">Profile</a></li>
</ul>
</nav>
</header>
Expand Down
24 changes: 24 additions & 0 deletions views/profile/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Profile</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<h1>Edit Profile</h1>
<form action="/update" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="{{.Username}}"><br><br>

<label for="email">Email:</label>
<input type="email" id="email" name="email" value="{{.Email}}"><br><br>

<label for="bio">Bio:</label><br>
<textarea id="bio" name="bio" rows="4" cols="50">{{.Bio}}</textarea><br><br>

<input type="submit" value="Save">
</form>
</body>
</html>