diff options
| author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-19 20:44:11 +0530 |
|---|---|---|
| committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-19 20:44:11 +0530 |
| commit | ff2d444d27eebfd093c2542eb798b1d4c708aab3 (patch) | |
| tree | 10364a01848039959ab9b2df4009ef993d6176d8 | |
| parent | 34d7bc9e99461cd275dd96330009dbe3ad17d082 (diff) | |
| -rw-r--r-- | config/config.go | 45 | ||||
| -rw-r--r-- | config/controller.go | 74 | ||||
| -rw-r--r-- | config/router.go | 31 | ||||
| -rw-r--r-- | config/service.go | 58 | ||||
| -rw-r--r-- | main.go | 8 |
5 files changed, 213 insertions, 3 deletions
diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..ffa8de3 --- /dev/null +++ b/config/config.go @@ -0,0 +1,45 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023-2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +// not to be confused with the conf package. +// this stores config that can be edited +// by the user, while the conf package +// handles the config (TOML, usually) file + +package config + +import ( + "gorm.io/gorm" + d "vidhukant.com/openbills/db" +) + +var db *gorm.DB + +func init() { + db = d.DB + + db.AutoMigrate(&Config{}) +} + +// config is stored in key-value pairs +// these may include instance settings, +// firm information, etc +type Config struct { + ID uint + Key string + Value string +} diff --git a/config/controller.go b/config/controller.go new file mode 100644 index 0000000..4343932 --- /dev/null +++ b/config/controller.go @@ -0,0 +1,74 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023-2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package config + +import ( + "github.com/gin-gonic/gin" + "net/http" + "strconv" + e "vidhukant.com/openbills/errors" +) + +func handleGetConfig(ctx *gin.Context) { + var config []Config + + err := getConfig(&config) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "data": config, + }) +} + +func handleSaveConfig(ctx *gin.Context) { + var config Config + ctx.Bind(&config) + + err := config.upsert() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, nil) +} + +func handleDelConfig(ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + var config Config + config.ID = uint(id) + + err = config.del() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, nil) +} diff --git a/config/router.go b/config/router.go new file mode 100644 index 0000000..8572038 --- /dev/null +++ b/config/router.go @@ -0,0 +1,31 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023-2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package config + +import ( + "github.com/gin-gonic/gin" +) + +func Routes(route *gin.RouterGroup) { + i := route.Group("/config") + { + i.GET("/", handleGetConfig) + i.POST("/", handleSaveConfig) + i.DELETE("/:id", handleDelConfig) + } +} diff --git a/config/service.go b/config/service.go new file mode 100644 index 0000000..51e9a00 --- /dev/null +++ b/config/service.go @@ -0,0 +1,58 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023-2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package config + +import ( + "vidhukant.com/openbills/errors" +) + +func getConfig(config *[]Config) error { + res := db.Find(&config) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + if res.RowsAffected == 0 { + return errors.ErrNotFound + } + + return nil +} + +func (c *Config) upsert() error { + res := db.Save(c) + // TODO: handle potential errors + return res.Error +} + +func (c *Config) del() error { + res := db.Where("id = ?", c.ID).Delete(c) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + if res.RowsAffected == 0 { + return errors.ErrNotFound + } + + return nil +} @@ -27,6 +27,7 @@ import ( // routes "vidhukant.com/openbills/auth" + "vidhukant.com/openbills/config" "vidhukant.com/openbills/customer" "vidhukant.com/openbills/invoice" "vidhukant.com/openbills/item" @@ -38,7 +39,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.24.0" +const OPENBILLS_VERSION = "v0.25.0" func init() { if !viper.GetBool("debug_mode") { @@ -64,10 +65,11 @@ func main() { protected := api.Group("/") protected.Use(auth.Authorize()) { - user.Routes(protected) + config.Routes(protected) customer.Routes(protected) - item.Routes(protected) invoice.Routes(protected) + item.Routes(protected) + user.Routes(protected) } log.Printf("\x1b[46m\x1b[30m[info]\x1b[0m Running OpenBills Server %s\n", OPENBILLS_VERSION) |