Skip to content

Commit 4f4eaad

Browse files
Merge pull request #27 from bit8bytes/login
Login
2 parents 21aa397 + 93fbe12 commit 4f4eaad

102 files changed

Lines changed: 4074 additions & 638 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ linters:
2828
- sqlclosecheck
2929
- rowserrcheck
3030
settings:
31+
cyclop:
32+
max-complexity: 13
3133
gosec:
3234
excludes:
3335
- G104 # Unhandled errors (errcheck handles this better)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ confirm:
1414
## run/web: run the web application with live reload
1515
.PHONY: run/web
1616
run/web:
17-
reflex -s -r '\.(go|tmpl|js)$$' -- go run -tags sqlite ./cmd/web serve
17+
reflex -s -r '\.(go|tmpl|js)$$' -- go run -tags sqlite ./cmd/web serve --max-orgs=2
1818

1919
## run/www: run the web application with live reload
2020
.PHONY: run/www

cmd/web/handlers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func (app *application) getNotFound(w http.ResponseWriter, r *http.Request) *htt
1616
return app.html.Render(w, r, http.StatusOK, pages.NotFound, app.html.TemplateData(r))
1717
}
1818

19+
func (app *application) getForbidden(w http.ResponseWriter, r *http.Request) *httperr.Error {
20+
return app.html.Render(w, r, http.StatusOK, pages.Forbidden, app.html.TemplateData(r))
21+
}
22+
1923
func (app *application) GetHealthz(_ context.Context) (*gen.HealthzResponse, error) {
2024
return &gen.HealthzResponse{
2125
Status: "ok",

cmd/web/handlers:equipment.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type equipmentData struct {
4545

4646
type equipmentPrintData struct {
4747
OrgID string
48-
OrgName string
48+
OrgDisplayName string
4949
Inventories []equipment.Equipment
5050
Query string
5151
Category string
@@ -301,7 +301,8 @@ func (app *application) getEquipmentItem(w http.ResponseWriter, r *http.Request)
301301
app.resolveItemURLs(item)
302302

303303
data := app.html.TemplateData(r)
304-
data.Form = &equipment.DetailsForm{}
304+
f := equipment.DetailsFormFromEquipment(item)
305+
data.Form = &f
305306
data.Data = equipmentItemData{OrgID: orgID, Item: item, ID: itemID, ActiveTab: "details"}
306307
return app.html.Render(w, r, http.StatusOK, pages.EquipmentDetail, data)
307308
}
@@ -363,6 +364,20 @@ func (app *application) postEquipmentItemProperties(w http.ResponseWriter, r *ht
363364
return &httperr.Error{Error: err, Message: "Bad request.", Code: http.StatusBadRequest}
364365
}
365366

367+
if !form.Validate() {
368+
item, err := app.services.equipment.GetByID(ctx, itemID)
369+
if err != nil {
370+
return &httperr.Error{Error: err, Message: "Failed to retrieve inventory item.", Code: http.StatusInternalServerError}
371+
}
372+
373+
app.resolveItemURLs(item)
374+
375+
data := app.html.TemplateData(r)
376+
data.Form = &form
377+
data.Data = equipmentItemData{OrgID: orgID, Item: item, ID: itemID, ActiveTab: "properties"}
378+
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.EquipmentProperties, data)
379+
}
380+
366381
if err := app.services.equipment.UpdateProperties(ctx, equipment.UpdateEquipmentProperties{
367382
ID: itemID,
368383
Properties: form.ToProperties(),
@@ -974,7 +989,7 @@ func (app *application) getEquipmentPrint(w http.ResponseWriter, r *http.Request
974989
ctx := r.Context()
975990
orgID := r.PathValue("org_id")
976991

977-
org, err := app.services.orgs.GetByID(ctx, orgID)
992+
org, err := app.services.orgs.Get(ctx, orgID)
978993
if err != nil {
979994
return &httperr.Error{Error: err, Message: "Failed to retrieve organization.", Code: http.StatusInternalServerError}
980995
}
@@ -1006,7 +1021,7 @@ func (app *application) getEquipmentPrint(w http.ResponseWriter, r *http.Request
10061021
data := app.html.TemplateData(r)
10071022
data.Data = equipmentPrintData{
10081023
OrgID: orgID,
1009-
OrgName: org.Name,
1024+
OrgDisplayName: org.DisplayName,
10101025
Inventories: filtered,
10111026
Query: query,
10121027
Category: category,

cmd/web/handlers:equipment:categories.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ type equipmentCategoriesData struct {
2424
}
2525

2626
type equipmentCategoryData struct {
27-
OrgID string
28-
Category *categories.EquipmentCategory
29-
ID string
27+
OrgID string
28+
ID string
3029
}
3130

3231
func (app *application) getEquipmentCategories(w http.ResponseWriter, r *http.Request) *httperr.Error {
@@ -124,8 +123,8 @@ func (app *application) getEquipmentCategory(w http.ResponseWriter, r *http.Requ
124123
}
125124

126125
data := app.html.TemplateData(r)
127-
data.Form = &categories.Form{}
128-
data.Data = equipmentCategoryData{OrgID: orgID, Category: category, ID: catID}
126+
data.Form = &categories.Form{Name: category.Name}
127+
data.Data = equipmentCategoryData{OrgID: orgID, ID: catID}
129128
return app.html.Render(w, r, http.StatusOK, pages.EquipmentCategoriesDetail, data)
130129
}
131130

@@ -139,18 +138,10 @@ func (app *application) postEquipmentCategory(w http.ResponseWriter, r *http.Req
139138
return &httperr.Error{Error: err, Message: "Bad request.", Code: http.StatusBadRequest}
140139
}
141140

142-
category, err := app.services.equipmentcategories.GetByID(ctx, catID)
143-
if err != nil {
144-
if errors.Is(err, database.ErrNotFound) {
145-
return &httperr.Error{Error: err, Message: "Equipment category not found.", Code: http.StatusNotFound}
146-
}
147-
return &httperr.Error{Error: err, Message: "Failed to retrieve equipment category.", Code: http.StatusInternalServerError}
148-
}
149-
150141
reRender := func(f *categories.Form) *httperr.Error {
151142
data := app.html.TemplateData(r)
152143
data.Form = f
153-
data.Data = equipmentCategoryData{OrgID: orgID, Category: category, ID: catID}
144+
data.Data = equipmentCategoryData{OrgID: orgID, ID: catID}
154145
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.EquipmentCategoriesDetail, data)
155146
}
156147

@@ -192,11 +183,11 @@ func (app *application) postDeleteEquipmentCategory(w http.ResponseWriter, r *ht
192183
}
193184
return &httperr.Error{Error: fetchErr, Message: "Failed to retrieve equipment category.", Code: http.StatusInternalServerError}
194185
}
195-
f := &categories.Form{}
186+
f := &categories.Form{Name: category.Name}
196187
f.AddError("delete", "Cannot delete: this category is assigned to one or more equipment items.")
197188
data := app.html.TemplateData(r)
198189
data.Form = f
199-
data.Data = equipmentCategoryData{OrgID: orgID, Category: category, ID: catID}
190+
data.Data = equipmentCategoryData{OrgID: orgID, ID: catID}
200191
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.EquipmentCategoriesDetail, data)
201192
}
202193
return &httperr.Error{

cmd/web/handlers:equipment:locations.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ type locationsData struct {
2424
}
2525

2626
type locationData struct {
27-
OrgID string
28-
Location *locations.Location
29-
ID string
27+
OrgID string
28+
ID string
3029
}
3130

3231
func (app *application) getLocations(w http.ResponseWriter, r *http.Request) *httperr.Error {
@@ -125,8 +124,8 @@ func (app *application) getLocation(w http.ResponseWriter, r *http.Request) *htt
125124
}
126125

127126
data := app.html.TemplateData(r)
128-
data.Form = &locations.Form{}
129-
data.Data = locationData{OrgID: orgID, Location: loc, ID: locID}
127+
data.Form = &locations.Form{Name: loc.Name}
128+
data.Data = locationData{OrgID: orgID, ID: locID}
130129
return app.html.Render(w, r, http.StatusOK, pages.LocationsDetail, data)
131130
}
132131

@@ -150,7 +149,7 @@ func (app *application) postLocation(w http.ResponseWriter, r *http.Request) *ht
150149
reRender := func(f *locations.Form) *httperr.Error {
151150
data := app.html.TemplateData(r)
152151
data.Form = f
153-
data.Data = locationData{OrgID: loc.OrgID, Location: loc, ID: locID}
152+
data.Data = locationData{OrgID: loc.OrgID, ID: locID}
154153
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.LocationsDetail, data)
155154
}
156155

cmd/web/handlers:equipment:manufacturers.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ type manufacturersData struct {
2424
}
2525

2626
type manufacturerData struct {
27-
OrgID string
28-
Manufacturer *manufacturers.Manufacturer
29-
ID string
27+
OrgID string
28+
ID string
3029
}
3130

3231
func (app *application) getManufacturers(w http.ResponseWriter, r *http.Request) *httperr.Error {
@@ -125,8 +124,8 @@ func (app *application) getManufacturer(w http.ResponseWriter, r *http.Request)
125124
}
126125

127126
data := app.html.TemplateData(r)
128-
data.Form = &manufacturers.Form{}
129-
data.Data = manufacturerData{OrgID: orgID, Manufacturer: manufacturer, ID: mfrID}
127+
data.Form = &manufacturers.Form{Name: manufacturer.Name}
128+
data.Data = manufacturerData{OrgID: orgID, ID: mfrID}
130129
return app.html.Render(w, r, http.StatusOK, pages.ManufacturersDetail, data)
131130
}
132131

@@ -150,7 +149,7 @@ func (app *application) postManufacturer(w http.ResponseWriter, r *http.Request)
150149
reRender := func(f *manufacturers.Form) *httperr.Error {
151150
data := app.html.TemplateData(r)
152151
data.Form = f
153-
data.Data = manufacturerData{OrgID: mfr.OrgID, Manufacturer: mfr, ID: mfrID}
152+
data.Data = manufacturerData{OrgID: mfr.OrgID, ID: mfrID}
154153
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.ManufacturersDetail, data)
155154
}
156155

@@ -193,11 +192,11 @@ func (app *application) postDeleteManufacturer(w http.ResponseWriter, r *http.Re
193192
}
194193
return &httperr.Error{Error: fetchErr, Message: "Failed to retrieve manufacturer.", Code: http.StatusInternalServerError}
195194
}
196-
f := &manufacturers.Form{}
195+
f := &manufacturers.Form{Name: manufacturer.Name}
197196
f.AddError("delete", "Cannot delete: this manufacturer is assigned to one or more inventory items.")
198197
data := app.html.TemplateData(r)
199198
data.Form = f
200-
data.Data = manufacturerData{OrgID: orgID, Manufacturer: manufacturer, ID: mfrID}
199+
data.Data = manufacturerData{OrgID: orgID, ID: mfrID}
201200
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.ManufacturersDetail, data)
202201
}
203202
return &httperr.Error{

0 commit comments

Comments
 (0)