Skip to content

Commit

Permalink
complete admin update reservation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ErfiDev committed Dec 6, 2021
1 parent 29183b7 commit 6f3afa8
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
90 changes: 90 additions & 0 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package controllers

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -539,3 +541,91 @@ func (r Repository) NewReservations(res http.ResponseWriter, req *http.Request)
func (r Repository) Admin(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/admin/dashboard", http.StatusSeeOther)
}

func (r Repository) SingleReservation(res http.ResponseWriter, req *http.Request) {
id, err := strconv.Atoi(chi.URLParam(req, "id"))
if err != nil {
utils.ServerError(res, err)
return
}

getReservation, errDb := r.DB.GetReservationById(id)
if errDb != nil {
utils.ServerError(res, errDb)
return
}

data := make(map[string]interface{})
data["reservation"] = getReservation
data["title"] = fmt.Sprintf("%s Reservation", getReservation.Email)
data["path"] = "/reservations"
data["id"] = id

utils.RenderTemplate(res, req, "admin-single-res.page.gohtml", &models.TmpData{
Data: data,
Form: forms.New(nil),
})
}

func (r Repository) ApiUpdateReservation(res http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
utils.ServerError(res, err)
return
}

form := forms.New(req.PostForm)

form.Has("email", req)
form.Has("first_name", req)
form.Has("last_name", req)
form.Has("phone", req)

idInt, _ := strconv.Atoi(req.Form.Get("id"))
reservation := models.Reservation{
Email: req.Form.Get("email"),
FirstName: req.Form.Get("first_name"),
LastName: req.Form.Get("last_name"),
Phone: req.Form.Get("phone"),
ID: idInt,
}

data := make(map[string]interface{})

if !form.Valid() {
data["reservation"] = reservation
data["title"] = "Update reservation"
data["path"] = "/reservations"

r.App.Session.Put(req.Context(), "error", "please fill correct input value!")
utils.RenderTemplate(res, req, "admin-single-res.page.gohtml", &models.TmpData{
Data: data,
Form: form,
})
return
}

result, errDb := r.DB.UpdateReservation(reservation)
if errDb != nil {
utils.ServerError(res, errDb)
return
} else {
if !result {
utils.ServerError(res, errors.New("we have problem on server!"))
return
}

allRes, errGetAllRes := r.DB.AllReservations()
if errGetAllRes != nil {
utils.ServerError(res, errGetAllRes)
return
}
data["reservations"] = allRes
data["title"] = "Update reservation"
data["path"] = "/reservations"
r.App.Session.Put(req.Context(), "flash", "update reservation success!")
utils.RenderTemplate(res, req, "admin-reservations.page.gohtml", &models.TmpData{
Data: data,
})
}
}
24 changes: 24 additions & 0 deletions repository/dbrepo/dbrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,27 @@ func (psdb postgresDbRepo) AllNewReservations() ([]models.Reservation, error) {

return reservations, nil
}

func (psdb postgresDbRepo) UpdateReservation(res models.Reservation) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `update reservations set
first_name = $1 , last_name = $2,
email = $3 , phone = $4
where id = $5;`

_, err := psdb.DB.ExecContext(ctx, query,
res.FirstName,
res.LastName,
res.Email,
res.Phone,
res.ID,
)

if err != nil {
return false, err
}

return true, err
}
4 changes: 4 additions & 0 deletions repository/dbrepo/test-db.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ func (psdb testDbRepo) GetReservationById(id int) (models.Reservation, error) {
func (psdb testDbRepo) AllNewReservations() ([]models.Reservation, error) {
return []models.Reservation{}, nil
}

func (psdb testDbRepo) UpdateReservation(res models.Reservation) (bool, error) {
return true, nil
}
1 change: 1 addition & 0 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ type DatabaseRepository interface {
AllReservations() ([]models.Reservation, error)
GetReservationById(id int) (models.Reservation, error)
AllNewReservations() ([]models.Reservation, error)
UpdateReservation(models.Reservation) (bool, error)
}
2 changes: 2 additions & 0 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func Routes() http.Handler {
mux.Get("/dashboard", controllers.Repo.AdminDashboard)
mux.Get("/reservations", controllers.Repo.AdminReservations)
mux.Get("/newReservations", controllers.Repo.NewReservations)
mux.Get("/reservation/{id}", controllers.Repo.SingleReservation)
mux.Post("/api/updateReservation", controllers.Repo.ApiUpdateReservation)
})

// POST routes
Expand Down
6 changes: 6 additions & 0 deletions static/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ body {

.dataTable-wrapper {
width: 100%;
}

.admin-single-res {
display: flex;
justify-content: center;
align-items: center;
}
58 changes: 58 additions & 0 deletions views/admin-single-res.page.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{template "admin" .}}

{{define "adminsubpage"}}
<div class="admin-single-res">
{{$res := index .Data "reservation"}}
<form action="/admin/api/updateReservation" method="POST" class="admin-single-res-form">
<input type="text" hidden name="csrf_token" value="{{.CSRF}}" />
<input name="id" value="{{.Data.id}}" hidden />

<div class="form-group mt-3">
<label for="email">Email:</label>
{{with .Form.Errors.Get "email"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "email"}} is-invalid {{end}}" id="email"
autocomplete="off" type='email'
name='email' value="{{with $res.Email}}{{.}}{{end}}">
</div>
<div class="form-group mt-3">
<label for="first_name">firstname:</label>
{{with .Form.Errors.Get "first_name"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "first_name"}} is-invalid {{end}}" id="first_name"
autocomplete="off" type='first_name'
name='first_name' value="{{with $res.FirstName}}{{.}}{{end}}">
</div>
<div class="form-group mt-3">
<label for="last_name">lastname:</label>
{{with .Form.Errors.Get "last_name"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "last_name"}} is-invalid {{end}}" id="last_name"
autocomplete="off" type='last_name'
name='last_name' value="{{with $res.LastName}}{{.}}{{end}}">
</div>
<div class="form-group mt-3">
<label for="phone">phone:</label>
{{with .Form.Errors.Get "phone"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class="form-control {{with .Form.Errors.Get "phone"}} is-invalid {{end}}" id="phone"
autocomplete="off" type='phone'
name='phone' value="{{with $res.Phone}}{{.}}{{end}}">
</div>

<button class="btn btn-primary" type="submit">Update Reservation</button>
<button class="cancel-update-res">Cancel</button>
</form>
</div>
<script defer>
const cancelBtn = document.querySelector(".cancel-update-res")

cancelBtn.addEventListener("click" , ()=> {
window.location.replace("http://localhost:3000/admin/reservations")
})
</script>
{{end}}
30 changes: 30 additions & 0 deletions views/admin.layout.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,35 @@
</div>
</section>
</body>
<script defer >
function Notify(msg = "" , icon = "info"){
const toast = Swal.mixin({
toast: true,
title: msg,
icon: icon,
position: "bottom-right",
showConfirmButton: false,
timer: 6000,
timerProgressBar: true,
didOpen: (toast)=> {
toast.addEventListener("mouseenter" , Swal.stopTimer)
toast.addEventListener("mouseleave" , Swal.resumeTimer)
}
})
toast.fire({})
}

{{with .Error}}
Notify("{{.}}" , "error")
{{end}}

{{with .Flash}}
Notify("{{.}}" , "success")
{{end}}

{{with .Warning}}
Notify("{{.}}" , "warning")
{{end}}
</script>
</html>
{{end}}

0 comments on commit 6f3afa8

Please sign in to comment.