Skip to content

Commit

Permalink
save license record button
Browse files Browse the repository at this point in the history
  • Loading branch information
vsimakhin committed Jan 28, 2025
1 parent f701330 commit a9cbd15
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 25 deletions.
141 changes: 122 additions & 19 deletions app/handlers_licensing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,128 @@ func (app *application) HandlerApiGetLicensingRecord(w http.ResponseWriter, r *h
app.writeJSON(w, http.StatusOK, license)
}

func (app *application) ParseLicenseFormData(r *http.Request) (license models.License, err error) {
err = r.ParseMultipartForm(32 << 20)
if err != nil {
app.errorLog.Println(fmt.Errorf("cannot parse the data, probably the attachment is too big - %s", err))
return license, err
}

license = models.License{
UUID: r.PostFormValue("uuid"),
Category: r.PostFormValue("category"),
Name: r.PostFormValue("name"),
Number: r.PostFormValue("number"),
Issued: r.PostFormValue("issued"),
ValidFrom: r.PostFormValue("valid_from"),
ValidUntil: r.PostFormValue("valid_until"),
Remarks: r.PostFormValue("remarks"),
}

// check attached file
file, header, err := r.FormFile("document")
if err != nil {
if !strings.Contains(err.Error(), "no such file") {
return license, err
}
} else {
defer file.Close()
license.DocumentName = header.Filename

// read file
bs, err := io.ReadAll(file)
if err != nil {
return license, err
}
license.Document = bs
}

return license, nil
}

// HandlerLicensingRecordSave is a handler for creating or updating license record
func (app *application) HandlerApiNewLicensingRecord(w http.ResponseWriter, r *http.Request) {

err := r.ParseMultipartForm(32 << 20)
if err != nil {
app.errorLog.Println(fmt.Errorf("cannot parse the data, probably the attachment is too big - %s", err))
app.handleError(w, err)
return
}

license := models.License{
UUID: r.PostFormValue("uuid"),
Category: r.PostFormValue("category"),
Name: r.PostFormValue("name"),
Number: r.PostFormValue("number"),
Issued: r.PostFormValue("issued"),
ValidFrom: r.PostFormValue("valid_from"),
ValidUntil: r.PostFormValue("valid_until"),
Remarks: r.PostFormValue("remarks"),
}

// check attached file
file, header, err := r.FormFile("document")
if err != nil {
if !strings.Contains(err.Error(), "no such file") {
app.handleError(w, err)
return
}
} else {
defer file.Close()
license.DocumentName = header.Filename

// read file
bs, err := io.ReadAll(file)
if err != nil {
app.handleError(w, err)
return
}
license.Document = bs
}

// new record
uuid, err := uuid.NewRandom()
if err != nil {
app.handleError(w, err)
return
}

license.UUID = uuid.String()

err = app.db.InsertLicenseRecord(license)
if err != nil {
app.handleError(w, err)
return
}

var response models.JSONResponse
response.OK = true
response.Message = "New License Record created"
response.Data = license.UUID

app.writeJSON(w, http.StatusOK, response)
}

// HandlerApiUpdateLicensingRecord is a handler for updating license record
func (app *application) HandlerApiUpdateLicensingRecord(w http.ResponseWriter, r *http.Request) {
license, err := app.ParseLicenseFormData(r)
if err != nil {
app.handleError(w, err)
return
}

// just update the license record
err = app.db.UpdateLicenseRecord(license)
if err != nil {
app.handleError(w, err)
return

}

app.writeOkResponse(w, "License Record has been updated")
}

//////////////////////////////////////////////
/////////////////////////////////////////////

Expand All @@ -58,25 +180,6 @@ func (app *application) HandlerLicensingDownload(w http.ResponseWriter, r *http.
}
}

// HandlerLicensingRecordNew is a handler for creating a new license record
func (app *application) HandlerLicensingRecordNew(w http.ResponseWriter, r *http.Request) {

var license models.License

categories, err := app.db.GetLicensesCategory()
if err != nil {
app.errorLog.Println(err)
}

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

if err := app.renderTemplate(w, r, "license-record", &templateData{Data: data}); err != nil {
app.errorLog.Println(err)
}
}

// HandlerLicensingRecordDelete is a handler for deleting license record
func (app *application) HandlerLicensingRecordDelete(w http.ResponseWriter, r *http.Request) {

Expand Down
3 changes: 2 additions & 1 deletion app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ func (app *application) routes() *chi.Mux {
r.Route("/licensing", func(r chi.Router) {
r.Get("/list", app.HandlerApiGetLicensingRecords)
r.With(middleware.Compress(5)).Get("/{uuid}", app.HandlerApiGetLicensingRecord)
r.Post("/new", app.HandlerApiNewLicensingRecord)
r.Put("/{uuid}", app.HandlerApiUpdateLicensingRecord)
})

// attachments
Expand Down Expand Up @@ -248,7 +250,6 @@ func (app *application) routes() *chi.Mux {
r.Get(APIMapData, app.HandlerMapData)

// documents
r.Get(APILicensingNew, app.HandlerLicensingRecordNew)
r.Get(APILicensingDownloadUUID, app.HandlerLicensingDownload)

r.Post(APILicensingSave, app.HandlerLicensingRecordSave)
Expand Down
128 changes: 128 additions & 0 deletions app/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"arc": "^0.1.4",
"dayjs": "^1.11.13",
"export-to-csv": "^1.4.0",
"file-type": "^20.0.0",
"material-react-table": "^3.0.3",
"ol": "^10.3.1",
"react": "^18.3.1",
Expand Down
Loading

0 comments on commit a9cbd15

Please sign in to comment.