cosmium/api/handlers/partition_key_ranges.go

50 lines
1.3 KiB
Go
Raw Normal View History

2024-02-10 23:44:20 +00:00
package handlers
import (
2024-02-14 18:14:10 +00:00
"fmt"
2024-02-10 23:44:20 +00:00
"net/http"
"github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
2024-02-10 23:44:20 +00:00
)
func GetPartitionKeyRanges(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
2024-02-14 18:14:10 +00:00
if c.Request.Header.Get("if-none-match") != "" {
c.AbortWithStatus(http.StatusNotModified)
return
}
2024-02-10 23:44:20 +00:00
partitionKeyRanges, status := repositories.GetPartitionKeyRanges(databaseId, collectionId)
if status == repositorymodels.StatusOk {
2024-02-14 18:14:10 +00:00
c.Header("etag", "\"420\"")
c.Header("lsn", "420")
c.Header("x-ms-cosmos-llsn", "420")
c.Header("x-ms-global-committed-lsn", "420")
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(partitionKeyRanges)))
2024-02-27 18:08:48 +00:00
collectionRid := collectionId
collection, _ := repositories.GetCollection(databaseId, collectionId)
2024-02-27 18:08:48 +00:00
if collection.ResourceID != "" {
collectionRid = collection.ResourceID
}
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusOK, gin.H{
2024-02-27 18:08:48 +00:00
"_rid": collectionRid,
2024-02-10 23:44:20 +00:00
"_count": len(partitionKeyRanges),
"PartitionKeyRanges": partitionKeyRanges,
})
return
}
if status == repositorymodels.StatusNotFound {
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}