Skip to content

Commit 08548ef

Browse files
committed
Adds: Applcation routes and schema
1 parent abafda7 commit 08548ef

File tree

3 files changed

+295
-0
lines changed

3 files changed

+295
-0
lines changed

internal/api/applications.go

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
8+
"github.com/sdslabs/nymeria/internal/database/applications"
9+
"github.com/sdslabs/nymeria/internal/database/schema"
10+
"github.com/sdslabs/nymeria/internal/utils"
11+
)
12+
13+
func HandleGetApplicationFlow(c *gin.Context) {
14+
csrfToken, err := utils.GenerateCSRFToken(c.GetHeader("X-User-ID")) // TODO: Get user ID from session
15+
if err != nil {
16+
c.JSON(http.StatusInternalServerError, gin.H{
17+
"status": "error",
18+
"message": "Failed to generate CSRF token",
19+
})
20+
return
21+
}
22+
23+
c.JSON(http.StatusOK, gin.H{
24+
"status": "success",
25+
"message": "create application flow",
26+
"csrf_token": csrfToken,
27+
})
28+
}
29+
30+
func HandleFetchAllApplicationsFlow(c *gin.Context) {
31+
apps, err := applications.GetAllApplications()
32+
33+
if err != nil {
34+
c.JSON(http.StatusInternalServerError, gin.H{
35+
"status": "error",
36+
"message": err.Error(),
37+
})
38+
return
39+
}
40+
41+
c.JSON(http.StatusOK, gin.H{
42+
"status": "success",
43+
"message": "applications fetched",
44+
"data": apps,
45+
})
46+
}
47+
48+
func HandleFetchApplicationByIDFlow(c *gin.Context) {
49+
id := c.Param("id")
50+
51+
app, err := applications.GetApplicationByID(id)
52+
53+
if err != nil {
54+
c.JSON(http.StatusInternalServerError, gin.H{
55+
"status": "error",
56+
"message": err.Error(),
57+
})
58+
return
59+
}
60+
61+
c.JSON(http.StatusOK, gin.H{
62+
"status": "success",
63+
"message": "application fetched",
64+
"data": app,
65+
})
66+
}
67+
68+
func HandleCreateApplicationFlow(c *gin.Context) {
69+
var req CreateApplicationRequest
70+
71+
err := c.ShouldBindJSON(&req)
72+
if err != nil {
73+
c.JSON(http.StatusBadRequest, gin.H{
74+
"status": "error",
75+
"message": err.Error(),
76+
})
77+
return
78+
}
79+
80+
clientKey, err := utils.GenerateRandomString(16)
81+
if err != nil {
82+
c.JSON(http.StatusInternalServerError, gin.H{
83+
"status": "error",
84+
"message": "Failed to generate client key",
85+
})
86+
return
87+
}
88+
89+
clientSecret, err := utils.GenerateRandomString(32)
90+
if err != nil {
91+
c.JSON(http.StatusInternalServerError, gin.H{
92+
"status": "error",
93+
"message": "Failed to generate client secret",
94+
})
95+
return
96+
}
97+
98+
app := schema.Application{
99+
Name: req.Name,
100+
AllowedOrigins: req.AllowedOrigins,
101+
RedirectURIs: req.RedirectURIs,
102+
ClientKey: clientKey,
103+
ClientSecret: clientSecret,
104+
}
105+
106+
err = applications.CreateApplication(app)
107+
if err != nil {
108+
c.JSON(http.StatusInternalServerError, gin.H{
109+
"status": "error",
110+
"message": err.Error(),
111+
})
112+
return
113+
}
114+
115+
c.JSON(http.StatusOK, gin.H{
116+
"status": "success",
117+
"message": "application created",
118+
"data": app,
119+
})
120+
}
121+
122+
func HandleDeleteApplicationFlow(c *gin.Context) {
123+
id := c.Param("id")
124+
125+
err := applications.DeleteApplication(id)
126+
if err != nil {
127+
c.JSON(http.StatusInternalServerError, gin.H{
128+
"status": "error",
129+
"message": err.Error(),
130+
})
131+
return
132+
}
133+
134+
c.JSON(http.StatusOK, gin.H{
135+
"status": "success",
136+
"message": "application deleted",
137+
})
138+
}
139+
140+
func HandleUpdateApplicationFlow(c *gin.Context) {
141+
var req UpdateApplicationRequest
142+
143+
err := c.ShouldBindJSON(&req)
144+
if err != nil {
145+
c.JSON(http.StatusBadRequest, gin.H{
146+
"status": "error",
147+
"message": err.Error(),
148+
})
149+
return
150+
}
151+
152+
app, err := applications.GetApplicationByID(req.ApplicationID)
153+
if err != nil {
154+
c.JSON(http.StatusInternalServerError, gin.H{
155+
"status": "error",
156+
"message": err.Error(),
157+
})
158+
return
159+
}
160+
161+
if req.NewKeyFlag {
162+
clientKey, err := utils.GenerateRandomString(16)
163+
if err != nil {
164+
c.JSON(http.StatusInternalServerError, gin.H{
165+
"status": "error",
166+
"message": err.Error(),
167+
})
168+
return
169+
}
170+
171+
clientSecret, err := utils.GenerateRandomString(32)
172+
if err != nil {
173+
c.JSON(http.StatusInternalServerError, gin.H{
174+
"status": "error",
175+
"message": err.Error(),
176+
})
177+
return
178+
}
179+
180+
app.ClientKey = clientKey
181+
app.ClientSecret = clientSecret
182+
}
183+
184+
if req.ApplicationURL != "" {
185+
app.ApplicationURL = req.ApplicationURL
186+
}
187+
188+
if len(req.AllowedOrigins) > 0 {
189+
app.AllowedOrigins = req.AllowedOrigins
190+
}
191+
192+
if len(req.RedirectURIs) > 0 {
193+
app.RedirectURIs = req.RedirectURIs
194+
}
195+
196+
err = applications.UpdateApplication(req.ApplicationID, app)
197+
198+
if err != nil {
199+
c.JSON(http.StatusInternalServerError, gin.H{
200+
"status": "error",
201+
"message": err.Error(),
202+
})
203+
return
204+
}
205+
206+
c.JSON(http.StatusOK, gin.H{
207+
"status": "success",
208+
"message": "application updated",
209+
"data": gin.H{
210+
"application_id": req.ApplicationID,
211+
"client_key": app.ClientKey,
212+
"client_secret": app.ClientSecret,
213+
},
214+
})
215+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package applications
2+
3+
import (
4+
"github.com/sdslabs/nymeria/internal/database"
5+
"github.com/sdslabs/nymeria/internal/database/schema"
6+
"github.com/sdslabs/nymeria/internal/logger"
7+
)
8+
9+
func GetAllApplications() ([]schema.Application, error) {
10+
var applications []schema.Application
11+
12+
result := database.DB.Find(&applications)
13+
if result.Error != nil {
14+
logger.Err(result.Error).Msg("failed to get applications")
15+
return nil, result.Error
16+
}
17+
18+
return applications, nil
19+
}
20+
21+
func CreateApplication(application schema.Application) error {
22+
result := database.DB.Create(&application)
23+
if result.Error != nil {
24+
logger.Err(result.Error).Msg("failed to create application")
25+
return result.Error
26+
}
27+
28+
return nil
29+
}
30+
31+
func GetApplicationByID(id string) (schema.Application, error) {
32+
var application schema.Application
33+
34+
result := database.DB.First(&application, "id = ?", id)
35+
if result.Error != nil {
36+
logger.Err(result.Error).Msg("failed to get application")
37+
return schema.Application{}, result.Error
38+
}
39+
40+
return application, nil
41+
}
42+
43+
func UpdateApplication(id string, application schema.Application) error {
44+
result := database.DB.Model(&schema.Application{}).Where("id = ?", id).Updates(application)
45+
if result.Error != nil {
46+
logger.Err(result.Error).Msg("failed to update application")
47+
return result.Error
48+
}
49+
50+
return nil
51+
}
52+
53+
func DeleteApplication(id string) error {
54+
result := database.DB.Delete(&schema.Application{}, "id = ?", id)
55+
if result.Error != nil {
56+
logger.Err(result.Error).Msg("failed to delete application")
57+
return result.Error
58+
}
59+
60+
return nil
61+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package schema
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
type Application struct {
10+
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"`
11+
Name string `gorm:"not null"`
12+
ApplicationURL string `gorm:"not null"`
13+
AllowedOrigins []string `gorm:"type:text[]"`
14+
RedirectURIs []string `gorm:"type:text[]"`
15+
ClientKey string `gorm:"not null;unique"`
16+
ClientSecret string `gorm:"not null"`
17+
Organizations []*Organization `gorm:"many2many:application_organizations;"`
18+
CreatedAt time.Time `gorm:"not null"`
19+
}

0 commit comments

Comments
 (0)