Skip to content

Commit

Permalink
✨ add holiday ical enpoint and swagger doku (#48)
Browse files Browse the repository at this point in the history
* ✨ add holiday ical enpoint and swagger doku

* 🐛 fix mime/type and holiday start and end

* ⬆️ fix client overview call and update go to 1.18
  • Loading branch information
jasperem authored May 5, 2022
1 parent 2f6d929 commit a85f3f6
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.18

- name: Build
run: CGO_ENABLED=0 go build -ldflags "-X main.version=${{ env.TAG }}" -o overtime main.go
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.18

- name: Build
run: go build -v ./...
Expand Down
11 changes: 6 additions & 5 deletions api/holiday.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/your-overtime/api/pkg"
"github.com/your-overtime/api/pkg/utils"
)

// CreateHoliday godoc
Expand Down Expand Up @@ -39,8 +40,8 @@ func (a *API) CreateHoliday(c *gin.Context) {
ho := pkg.Holiday{
UserID: user.ID,
InputHoliday: pkg.InputHoliday{
Start: ih.Start,
End: ih.End,
Start: utils.DayStart(ih.Start),
End: utils.DayEnd(ih.End),
Type: ih.Type,
Description: ih.Description,
},
Expand Down Expand Up @@ -84,8 +85,8 @@ func (a *API) UpdateHoliday(c *gin.Context) {
ho := pkg.Holiday{
ID: uint(id),
InputHoliday: pkg.InputHoliday{
Start: ih.Start,
End: ih.End,
Start: utils.DayStart(ih.Start),
End: utils.DayEnd(ih.End),
Type: ih.Type,
Description: ih.Description,
},
Expand Down Expand Up @@ -155,7 +156,7 @@ func (a *API) GetHolidays(c *gin.Context) {
c.JSON(http.StatusBadRequest, err)
return
}
h := []pkg.Holiday{}
var h []pkg.Holiday
typeStr := c.Query("type")
if len(typeStr) > 0 {
hType, err := pkg.StrToHolidayType(typeStr)
Expand Down
109 changes: 105 additions & 4 deletions api/ical.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,58 @@ import (
log "github.com/sirupsen/logrus"
)

// ICalActivities godoc
// @Tags activities.ics
// @Summary Get a activities by start and end
// @Produce text/calendar
// @Param start query string true "Start date" format date-time deafault 01.01 of the current year
// @Param end query string true "End date" format date-time default now
// @Success 200 activities as ical
// @Router /activities.ics [get]
// @Security BasicAuth
// @Security ApiKeyAuth
func (a *API) ICalActivities(c *gin.Context) {
yot, err := a.getOvertimeServiceForUserFromRequest(c)
if err != nil {
c.String(http.StatusUnauthorized, "401 Unauthorized")
}

var (
start time.Time
end time.Time
)

now := time.Now()
end := time.Date(now.Year(), 12, 31, 23, 59, 59, 0, now.Location())
start := time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, now.Location())
activitites, err := yot.GetActivities(start, end)
if len(c.Query("start")) > 0 {
start, err = time.Parse(time.RFC3339, c.Query("start"))
if err != nil {
log.Debug(err)
c.JSON(http.StatusBadRequest, err)
return
}
} else {
start = time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, now.Location())
}

if len(c.Query("end")) > 0 {
end, err = time.Parse(time.RFC3339, c.Query("end"))
if err != nil {
log.Debug(err)
c.JSON(http.StatusBadRequest, err)
return
}
} else {
end = time.Date(now.Year(), 12, 31, 23, 59, 59, 0, now.Location())
}

activities, err := yot.GetActivities(start, end)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
cal := ical.NewCalendar()
cal.Props.SetText(ical.PropProductID, "-//Your Overtime//Activities")
cal.Props.SetText(ical.PropVersion, "2.0")
for _, ac := range activitites {
for _, ac := range activities {
event := ical.NewEvent()
end := ac.End
if end == nil {
Expand All @@ -44,3 +79,69 @@ func (a *API) ICalActivities(c *gin.Context) {
log.Debugln(err)
}
}

// ICalHolidays godoc
// @Tags holidays.ics
// @Summary Get a holidays by start and end
// @Produce text/calendar
// @Param start query string true "Start date" format date-time deafault 01.01 of the current year
// @Param end query string true "End date" format date-time default now
// @Success 200 holidays as ical
// @Router /holidays.ics [get]
// @Security BasicAuth
// @Security ApiKeyAuth
func (a *API) ICalHolidays(c *gin.Context) {
yot, err := a.getOvertimeServiceForUserFromRequest(c)
if err != nil {
c.String(http.StatusUnauthorized, "401 Unauthorized")
}

var (
start time.Time
end time.Time
)

now := time.Now()
if len(c.Query("start")) > 0 {
start, err = time.Parse(time.RFC3339, c.Query("start"))
if err != nil {
log.Debug(err)
c.JSON(http.StatusBadRequest, err)
return
}
} else {
start = time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, now.Location())
}

if len(c.Query("end")) > 0 {
end, err = time.Parse(time.RFC3339, c.Query("end"))
if err != nil {
log.Debug(err)
c.JSON(http.StatusBadRequest, err)
return
}
} else {
end = time.Date(now.Year(), 12, 31, 23, 59, 59, 0, now.Location())
}

holidays, err := yot.GetHolidays(start, end)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
cal := ical.NewCalendar()
cal.Props.SetText(ical.PropProductID, "-//Your Overtime//Holidays")
cal.Props.SetText(ical.PropVersion, "2.0")
for _, h := range holidays {
event := ical.NewEvent()
event.Props.SetText(ical.PropUID, fmt.Sprintf("%d", h.ID))
event.Props.SetDateTime(ical.PropDateTimeStart, h.Start)
event.Props.SetDateTime(ical.PropDateTimeEnd, h.End)
event.Props.SetDateTime(ical.PropDateTimeStamp, now)
event.Props.SetText(ical.PropSummary, fmt.Sprintf("%s (%s)", h.Description, h.Type))
cal.Children = append(cal.Children, event.Component)
}
err = ical.NewEncoder(c.Writer).Encode(cal)
if err != nil {
log.Debugln(err)
}
}
92 changes: 92 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,49 @@ var doc = `{
}
}
},
"/activities.ics": {
"get": {
"security": [
{
"BasicAuth": []
},
{
"ApiKeyAuth": []
}
],
"produces": [
"text/calendar"
],
"tags": [
"activities.ics"
],
"summary": "Get a activities by start and end",
"parameters": [
{
"type": "string",
"description": "Start date",
"name": "start",
"in": "query",
"required": true
},
{
"type": "string",
"description": "End date",
"name": "end",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "activities"
}
}
}
}
},
"/activity": {
"get": {
"security": [
Expand Down Expand Up @@ -541,6 +584,49 @@ var doc = `{
}
}
},
"/holidays.ics": {
"get": {
"security": [
{
"BasicAuth": []
},
{
"ApiKeyAuth": []
}
],
"produces": [
"text/calendar"
],
"tags": [
"holidays.ics"
],
"summary": "Get a holidays by start and end",
"parameters": [
{
"type": "string",
"description": "Start date",
"name": "start",
"in": "query",
"required": true
},
{
"type": "string",
"description": "End date",
"name": "end",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "holidays"
}
}
}
}
},
"/overview": {
"get": {
"security": [
Expand Down Expand Up @@ -960,6 +1046,9 @@ var doc = `{
"properties": {
"Name": {
"type": "string"
},
"Readonly": {
"type": "boolean"
}
}
},
Expand Down Expand Up @@ -1065,6 +1154,9 @@ var doc = `{
"Name": {
"type": "string"
},
"Readonly": {
"type": "boolean"
},
"Token": {
"type": "string"
},
Expand Down
Loading

0 comments on commit a85f3f6

Please sign in to comment.