Skip to content
This repository was archived by the owner on Mar 12, 2023. It is now read-only.

Commit a537614

Browse files
committed
backender & lesson crud
1 parent 3e711f1 commit a537614

23 files changed

+746
-240
lines changed

cmd/course-api/main.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"github.com/kamp-us/course-api/internal/backend"
45
"log"
56
"net/http"
67
"os"
@@ -24,12 +25,15 @@ func main() {
2425
log.Fatal("error while creating a db connection pool", err)
2526
}
2627

27-
models.AutoMigrate(dbClient)
28-
29-
server := &server.CourseAPIServer{
30-
Db: dbClient,
28+
err = models.AutoMigrate(dbClient)
29+
if err != nil {
30+
return
3131
}
32-
twirpHandler := courseapi.NewCourseAPIServer(server)
32+
33+
postgreSQLBackend := backend.NewPostgreSQLBackend(dbClient)
34+
35+
s := server.NewCourseAPIServer(postgreSQLBackend)
36+
twirpHandler := courseapi.NewCourseAPIServer(s)
3337

3438
mux := http.NewServeMux()
3539
mux.Handle(twirpHandler.PathPrefix(), twirpHandler)

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ require (
2424
gorm.io/gorm v1.22.4
2525
)
2626

27-
require github.com/gofrs/uuid v4.0.0+incompatible
28-
2927
require github.com/gosimple/unidecode v1.0.1 // indirect

internal/backend/backend.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/kamp-us/course-api/internal/models"
6+
"gorm.io/gorm"
7+
)
8+
9+
type Backender interface {
10+
GetCourse(ctx context.Context, id string) (*models.Course, error)
11+
CreateCourse(ctx context.Context, userID string, name string, description string) (*models.Course, error)
12+
UpdateCourse(ctx context.Context, id string, name *string, description *string) error
13+
DeleteCourse(ctx context.Context, id string) error
14+
GetLesson(ctx context.Context, id string) (*models.Lesson, error)
15+
CreateLesson(ctx context.Context, userID string, name string, description string, courseID string) (*models.Lesson, error)
16+
UpdateLesson(ctx context.Context, id string, name *string, description *string) error
17+
DeleteLesson(ctx context.Context, id string) error
18+
}
19+
20+
type PostgreSQLBackend struct {
21+
DB *gorm.DB
22+
}
23+
24+
func NewPostgreSQLBackend(db *gorm.DB) Backender {
25+
return &PostgreSQLBackend{
26+
DB: db,
27+
}
28+
}

internal/backend/create_course.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/gosimple/slug"
6+
"github.com/kamp-us/course-api/internal/models"
7+
)
8+
9+
func (b *PostgreSQLBackend) CreateCourse(ctx context.Context, userID string, name string, description string) (*models.Course, error) {
10+
course := models.Course{
11+
Slug: slug.MakeLang(name, "tr"),
12+
Name: name,
13+
Description: description,
14+
UserID: userID,
15+
}
16+
17+
result := b.DB.Create(&course)
18+
if result.Error != nil {
19+
return nil, result.Error
20+
}
21+
22+
return &course, nil
23+
}

internal/backend/create_lesson.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/gosimple/slug"
6+
"github.com/kamp-us/course-api/internal/models"
7+
)
8+
9+
func (b *PostgreSQLBackend) CreateLesson(ctx context.Context, userID string, name string, description string, courseID string) (*models.Lesson, error) {
10+
lesson := models.Lesson{
11+
Slug: slug.MakeLang(name, "tr"),
12+
Name: name,
13+
Description: description,
14+
UserID: userID,
15+
CourseID: courseID,
16+
}
17+
18+
result := b.DB.Create(&lesson)
19+
if result.Error != nil {
20+
return nil, result.Error
21+
}
22+
23+
return &lesson, nil
24+
}

internal/backend/delete_course.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/kamp-us/course-api/internal/models"
6+
)
7+
8+
func (b *PostgreSQLBackend) DeleteCourse(ctx context.Context, id string) error {
9+
course := models.Course{}
10+
result := b.DB.First(&course, "id = ?", id)
11+
if result.Error != nil {
12+
return result.Error
13+
}
14+
15+
result = b.DB.Delete(&course)
16+
if result.Error != nil {
17+
return result.Error
18+
}
19+
20+
return nil
21+
}

internal/backend/delete_lesson.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/kamp-us/course-api/internal/models"
6+
)
7+
8+
func (b *PostgreSQLBackend) DeleteLesson(ctx context.Context, id string) error {
9+
lesson := models.Lesson{}
10+
if query := b.DB.First(&lesson, "id = ?", id); query.Error != nil {
11+
return query.Error
12+
}
13+
result := b.DB.Delete(&lesson)
14+
if result.Error != nil {
15+
return result.Error
16+
}
17+
18+
return nil
19+
}

internal/backend/get_course.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/kamp-us/course-api/internal/models"
6+
)
7+
8+
func (b *PostgreSQLBackend) GetCourse(ctx context.Context, id string) (*models.Course, error) {
9+
course := models.Course{}
10+
result := b.DB.First(&course, "id = ?", id)
11+
if result.Error != nil {
12+
return nil, result.Error
13+
}
14+
15+
query := b.DB.Model(&course).Association("Categories")
16+
if query.Error != nil {
17+
return nil, query.Error
18+
}
19+
20+
return &course, nil
21+
}

internal/backend/get_lesson.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/kamp-us/course-api/internal/models"
6+
)
7+
8+
func (b *PostgreSQLBackend) GetLesson(ctx context.Context, id string) (*models.Lesson, error) {
9+
lesson := models.Lesson{}
10+
11+
result := b.DB.First(&lesson, "id = ?", id)
12+
if result.Error != nil {
13+
return nil, result.Error
14+
}
15+
16+
return &lesson, nil
17+
}

internal/backend/update_course.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/gosimple/slug"
6+
"github.com/kamp-us/course-api/internal/models"
7+
)
8+
9+
func (b *PostgreSQLBackend) UpdateCourse(ctx context.Context, id string, name *string, description *string) error {
10+
course := models.Course{}
11+
result := b.DB.First(&course, "id = ?", id)
12+
if result.Error != nil {
13+
return result.Error
14+
}
15+
16+
updates := models.Course{Name: *name, Slug: slug.MakeLang(*name, "tr"), Description: *description}
17+
result = b.DB.Model(&course).Updates(updates)
18+
if result.Error != nil {
19+
return result.Error
20+
}
21+
22+
return nil
23+
}

internal/backend/update_lesson.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package backend
2+
3+
import (
4+
"context"
5+
"github.com/gosimple/slug"
6+
"github.com/kamp-us/course-api/internal/models"
7+
)
8+
9+
func (b *PostgreSQLBackend) UpdateLesson(ctx context.Context, id string, name *string, description *string) error {
10+
lesson := models.Lesson{}
11+
if result := b.DB.First(&lesson, "id = ?", id); result.Error != nil {
12+
return result.Error
13+
}
14+
15+
updates := models.Lesson{Name: *name, Slug: slug.MakeLang(*name, "tr"), Description: *description}
16+
17+
if result := b.DB.Model(&lesson).Updates(updates); result.Error != nil {
18+
return result.Error
19+
}
20+
21+
return nil
22+
}

rpc/course-api/service.pb.go

+43-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/course-api/service.proto

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import "google/protobuf/empty.proto";
99
service CourseAPI {
1010
rpc GetCourse(GetCourseRequest) returns (Course);
1111
rpc CreateCourse(CreateCourseRequest) returns (Course);
12-
rpc UpdateCourse(UpdateCourseRequest) returns (Course);
12+
rpc UpdateCourse(UpdateCourseRequest) returns (google.protobuf.Empty);
1313
rpc DeleteCourse(DeleteCourseRequest) returns (google.protobuf.Empty);
1414
rpc CreateLesson(CreateLessonRequest) returns (Lesson);
1515
rpc GetLesson(GetLessonRequest) returns (Lesson);
16+
rpc UpdateLesson(UpdateLessonRequest) returns (google.protobuf.Empty);
1617
rpc DeleteLesson(DeleteLessonRequest) returns (google.protobuf.Empty);
1718
}
1819

0 commit comments

Comments
 (0)