Skip to content

Commit

Permalink
repositoryパッケージで共有
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Dec 27, 2024
1 parent b599284 commit 7f0f9f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
8 changes: 8 additions & 0 deletions api/externals/repository/dialect_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package repository

import (
goqu "github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
)

var dialect = goqu.Dialect("mysql")
10 changes: 0 additions & 10 deletions api/externals/repository/sponsor_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package repository
import (
"context"
"database/sql"
"fmt"

"github.com/NUTFes/FinanSu/api/drivers/db"
"github.com/NUTFes/FinanSu/api/externals/repository/abstract"
goqu "github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
)

type sponsorRepository struct {
Expand All @@ -32,7 +30,6 @@ func NewSponsorRepository(c db.Client, ac abstract.Crud) SponsorRepository {

// 全件取得
func (sr *sponsorRepository) All(c context.Context) (*sql.Rows, error) {
dialect := goqu.Dialect("mysql")
query, _, err := dialect.From("sponsors").ToSQL()
if err != nil {
return nil, err
Expand All @@ -42,9 +39,7 @@ func (sr *sponsorRepository) All(c context.Context) (*sql.Rows, error) {

// 1件取得
func (sr *sponsorRepository) Find(c context.Context, id string) (*sql.Row, error) {
dialect := goqu.Dialect("mysql")
query, _, err := dialect.From("sponsors").Where(goqu.Ex{"id": id}).ToSQL()
fmt.Println(query)
if err != nil {
return nil, err
}
Expand All @@ -60,7 +55,6 @@ func (sr *sponsorRepository) Create(
address string,
representative string,
) error {
dialect := goqu.Dialect("mysql")
ds := dialect.Insert("sponsors").Rows(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative})
query, _, err := ds.ToSQL()
if err != nil {
Expand All @@ -79,7 +73,6 @@ func (sr *sponsorRepository) Update(
address string,
representative string,
) error {
dialect := goqu.Dialect("mysql")
ds := dialect.Update("sponsors").Set(goqu.Record{"name": name, "tel": tel, "email": email, "address": address, "representative": representative}).Where(goqu.Ex{"id": id})
query, _, err := ds.ToSQL()
if err != nil {
Expand All @@ -93,7 +86,6 @@ func (sr *sponsorRepository) Delete(
c context.Context,
id string,
) error {
dialect := goqu.Dialect("mysql")
ds := dialect.Delete("sponsors").Where(goqu.Ex{"id": id})
query, _, err := ds.ToSQL()
if err != nil {
Expand All @@ -105,7 +97,6 @@ func (sr *sponsorRepository) Delete(

// 最新のsponcerを取得する
func (sr *sponsorRepository) FindLatestRecord(c context.Context) (*sql.Row, error) {
dialect := goqu.Dialect("mysql")
query, _, err := dialect.From("sponsors").Order(goqu.I("id").Desc()).Limit(1).ToSQL()
if err != nil {
return nil, err
Expand All @@ -115,7 +106,6 @@ func (sr *sponsorRepository) FindLatestRecord(c context.Context) (*sql.Row, erro

// 年度別に取得
func (sr *sponsorRepository) AllByPeriod(c context.Context, year string) (*sql.Rows, error) {
dialect := goqu.Dialect("mysql")
query, _, err := dialect.Select("sponsors.*").From("sponsors").InnerJoin(goqu.I("year_periods"), goqu.On(goqu.I("sponsors.created_at").Gt(goqu.I("year_periods.started_at")), goqu.I("sponsors.created_at").Lt(goqu.I("year_periods.ended_at")))).InnerJoin(goqu.I("years"), goqu.On(goqu.I("year_periods.year_id").Eq(goqu.I("years.id")))).Where(goqu.Ex{"years.year": year}).Order(goqu.I("sponsors.id").Desc()).ToSQL()
if err != nil {
return nil, err
Expand Down

0 comments on commit 7f0f9f9

Please sign in to comment.