Skip to content

Commit

Permalink
fix:FilterDurationを使うように
Browse files Browse the repository at this point in the history
  • Loading branch information
mathsuky committed Oct 31, 2024
1 parent adde449 commit 3ba23d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 65 deletions.
71 changes: 9 additions & 62 deletions domain/filter/shorthand.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,80 +54,27 @@ func FilterTime(start, end time.Time) Expr {
if start.IsZero() && end.IsZero() {
return nil
}

// startがゼロの時はend以前に一部でもイベントが含まれるものを返す
if start.IsZero() {
return &CmpExpr{
Attr: AttrTimeStart,
Relation: LessEq,
Value: end,
}
}
// endがゼロの時はstart以降に一部でもイベントが含まれるものを返す
if end.IsZero() {
return &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: start,
}
}

// イベント開始時刻が指定された範囲内にあるか
eventStartInRangeRight := &CmpExpr{
Attr: AttrTimeStart,
Relation: LessEq,
Value: end,
}
eventStartInRangeLeft := &CmpExpr{
timeStart := &CmpExpr{
Attr: AttrTimeStart,
Relation: GreterEq,
Value: start,
}

// イベント終了時刻が指定された範囲内にあるか
eventEndInRangeRight := &CmpExpr{
timeEnd := &CmpExpr{
Attr: AttrTimeEnd,
Relation: LessEq,
Value: end,
}
eventEndInRangeLeft := &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: start,
}

// イベントの開催期間が指定された範囲を包含しているか
eventStartBeforeRangeStart := &CmpExpr{
Attr: AttrTimeStart,
Relation: LessEq,
Value: start,
if start.IsZero() {
return timeEnd
}
eventEndAfterRangeEnd := &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: end,
if end.IsZero() {
return timeStart
}

return &LogicOpExpr{
LogicOp: Or,
Lhs: &LogicOpExpr{
LogicOp: Or,
Lhs: &LogicOpExpr{
LogicOp: And,
Lhs: eventStartInRangeRight,
Rhs: eventStartInRangeLeft,
},
Rhs: &LogicOpExpr{
LogicOp: And,
Lhs: eventEndInRangeRight,
Rhs: eventEndInRangeLeft,
},
},
Rhs: &LogicOpExpr{
LogicOp: And,
Lhs: eventStartBeforeRangeStart,
Rhs: eventEndAfterRangeEnd,
},
LogicOp: And,
Lhs: timeStart,
Rhs: timeEnd,
}
}

Expand Down
12 changes: 9 additions & 3 deletions router/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ func (h *Handlers) HandleGetEvents(c echo.Context) error {
if err != nil {
return badRequest(err, message("invalid time"))
}
events, err := h.Repo.GetEvents(
filter.AddAnd(expr, filter.FilterTime(start, end)),
getConinfo(c))

durationExpr, err := filter.FilterDuration(start, end)
if err != nil {
return badRequest(err, message("filter duration error"))
}

combinedExpr := filter.AddAnd(expr, durationExpr)

events, err := h.Repo.GetEvents(combinedExpr, getConinfo(c))
if err != nil {
return judgeErrorResponse(err)
}
Expand Down

0 comments on commit 3ba23d4

Please sign in to comment.