-
Notifications
You must be signed in to change notification settings - Fork 1
/
ext_character.go
48 lines (42 loc) · 1.37 KB
/
ext_character.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package artext
import (
"fmt"
"github.com/flandiayingman/arkwaifu/internal/app/art"
"github.com/gofiber/fiber/v2"
"github.com/pkg/errors"
"regexp"
"strconv"
)
func parseCharacterID(characterID string) (base string, body int, face int, err error) {
// Regex Pattern like: {BASE}#{FACE}${BODY}
pattern := regexp.MustCompile(`(\w+)#(\d+)\$(\d+)`)
matches := pattern.FindStringSubmatch(characterID)
if matches == nil {
return "", 0, 0, errors.Errorf("invalid character ID %s", characterID)
}
base = matches[1]
face, err = strconv.Atoi(matches[2])
body, err = strconv.Atoi(matches[3])
if err != nil {
return "", 0, 0, errors.Errorf("invalid character ID %s: %w", characterID, err)
}
return base, body, face, nil
}
// GetSiblingsOfCharacterArt gets the sibling characters of the specified character.
//
// Sibling Characters: the characters that have the same base name as the specified character.
func (s *Service) GetSiblingsOfCharacterArt(characterID string) (siblings []*art.Art, err error) {
base, _, _, err := parseCharacterID(characterID)
if err != nil {
return nil, err
}
return s.art.SelectArtsByIDLike(fmt.Sprintf("%s%%", base))
}
func (c *Controller) GetSiblingsOfCharacterArt(ctx *fiber.Ctx) error {
characterID := ctx.Params("id")
siblings, err := c.service.GetSiblingsOfCharacterArt(characterID)
if err != nil {
return err
}
return ctx.JSON(siblings)
}