Skip to content

Commit

Permalink
refactor(api): implement centralized tag collection
Browse files Browse the repository at this point in the history
Create a dedicated `tags` collection to centralize tag management across
the application. This replaces the previous string-based tag association
with a reference-based model using tag IDs.

The schema for a tag is as follow:
```json
{
    "_id": ObjectId,
    "tenant_id": String,
    "created_at": Time,
    "updated_at": Time,
    "name": String
}
```

Update all tag-related collections to use tag IDs instead of tag names.
A migration handles the conversion of existing tag data to the new
format.

API response format now includes tag objects:
```json
{
    ...
    "tags": [
        { "name": String },
        { "name": String },
        { "name": String }
    ]
}
```

Implement generic tag management methods in the store layer to handle
tag operations (push/pull) consistently across all taggable collections.
Add new query options to filter items by tags.

Introduce dual tag representation in taggable entities:
- TagsID: Internal array of tag IDs (not exposed via API)
- Tags: Array of models.Tag objects for API responses
  • Loading branch information
heiytor committed Jan 27, 2025
1 parent f607c23 commit 91c3b13
Show file tree
Hide file tree
Showing 57 changed files with 5,298 additions and 5,329 deletions.
54 changes: 0 additions & 54 deletions api/routes/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const (
OfflineDeviceURL = "/devices/:uid/offline"
LookupDeviceURL = "/lookup"
UpdateDeviceStatusURL = "/devices/:uid/:status"
CreateTagURL = "/devices/:uid/tags" // Add a tag to a device.
UpdateTagURL = "/devices/:uid/tags" // Update device's tags with a new set.
RemoveTagURL = "/devices/:uid/tags/:tag" // Delete a tag from a device.
UpdateDevice = "/devices/:uid"
)

Expand Down Expand Up @@ -243,57 +240,6 @@ func (h *Handler) UpdateDeviceStatus(c gateway.Context) error {
return c.NoContent(http.StatusOK)
}

func (h *Handler) CreateDeviceTag(c gateway.Context) error {
var req requests.DeviceCreateTag
if err := c.Bind(&req); err != nil {
return err
}

if err := c.Validate(&req); err != nil {
return err
}

if err := h.service.CreateDeviceTag(c.Ctx(), models.UID(req.UID), req.Tag); err != nil {
return err
}

return c.NoContent(http.StatusOK)
}

func (h *Handler) RemoveDeviceTag(c gateway.Context) error {
var req requests.DeviceRemoveTag
if err := c.Bind(&req); err != nil {
return err
}

if err := c.Validate(&req); err != nil {
return err
}

if err := h.service.RemoveDeviceTag(c.Ctx(), models.UID(req.UID), req.Tag); err != nil {
return err
}

return c.NoContent(http.StatusOK)
}

func (h *Handler) UpdateDeviceTag(c gateway.Context) error {
var req requests.DeviceUpdateTag
if err := c.Bind(&req); err != nil {
return err
}

if err := c.Validate(&req); err != nil {
return err
}

if err := h.service.UpdateDeviceTag(c.Ctx(), models.UID(req.UID), req.Tags); err != nil {
return err
}

return c.NoContent(http.StatusOK)
}

func (h *Handler) UpdateDevice(c gateway.Context) error {
var req requests.DeviceUpdate
if err := c.Bind(&req); err != nil {
Expand Down
Loading

0 comments on commit 91c3b13

Please sign in to comment.