-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
949 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/mylxsw/adanos-alert/internal/repository" | ||
"github.com/mylxsw/container" | ||
"github.com/mylxsw/hades" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type GroupController struct { | ||
cc *container.Container | ||
} | ||
|
||
func NewGroupController(cc *container.Container) hades.Controller { | ||
return &GroupController{cc: cc} | ||
} | ||
|
||
func (g GroupController) Register(router *hades.Router) { | ||
router.Group("/groups/", func(router *hades.Router) { | ||
router.Get("/", g.Groups).Name("groups:all") | ||
router.Get("/{id}/", g.Group).Name("groups:one") | ||
}) | ||
} | ||
|
||
type GroupsResp struct { | ||
Groups []repository.MessageGroup `json:"groups"` | ||
Next int64 `json:"next"` | ||
} | ||
|
||
// Groups list all message groups | ||
// Arguments: | ||
// - offset/limit | ||
// - status | ||
// - rule_id | ||
func (g GroupController) Groups(ctx hades.Context, groupRepo repository.MessageGroupRepo) (*GroupsResp, error) { | ||
offset := ctx.Int64Input("offset", 0) | ||
limit := ctx.Int64Input("limit", 10) | ||
|
||
filter := bson.M{} | ||
|
||
status := ctx.Input("status") | ||
if status != "" { | ||
filter["status"] = status | ||
} | ||
|
||
ruleID, err := primitive.ObjectIDFromHex(ctx.Input("rule_id")) | ||
if err == nil { | ||
filter["rule._id"] = ruleID | ||
} | ||
|
||
grps, next, err := groupRepo.Paginate(filter, offset, limit) | ||
if err != nil { | ||
return nil, hades.WrapJSONError(err, http.StatusInternalServerError) | ||
} | ||
|
||
return &GroupsResp{ | ||
Groups: grps, | ||
Next: next, | ||
}, nil | ||
} | ||
|
||
type GroupResp struct { | ||
Group repository.MessageGroup `json:"group"` | ||
Messages []repository.Message `json:"messages"` | ||
Next int64 `json:"next"` | ||
} | ||
|
||
func (g GroupController) Group( | ||
ctx hades.Context, | ||
groupRepo repository.MessageGroupRepo, | ||
messageRepo repository.MessageRepo, | ||
) (*GroupResp, error) { | ||
offset := ctx.Int64Input("offset", 0) | ||
limit := ctx.Int64Input("limit", 10) | ||
|
||
groupID, err := primitive.ObjectIDFromHex(ctx.PathVar("id")) | ||
if err != nil { | ||
return nil, hades.WrapJSONError(err, http.StatusUnprocessableEntity) | ||
} | ||
|
||
grp, err := groupRepo.Get(groupID) | ||
if err != nil { | ||
return nil, hades.WrapJSONError(err, http.StatusInternalServerError) | ||
} | ||
|
||
filter := messagesFilter(ctx) | ||
filter["group_ids"] = bson.M{"$in": groupID} | ||
|
||
messages, next, err := messageRepo.Paginate(filter, offset, limit) | ||
if err != nil { | ||
return nil, hades.WrapJSONError(err, http.StatusInternalServerError) | ||
} | ||
|
||
return &GroupResp{ | ||
Group: grp, | ||
Messages: messages, | ||
Next: next, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.