Skip to content

Commit

Permalink
Add images to videos and courses (#40)
Browse files Browse the repository at this point in the history
* Add images to courses

* Add images to videos

* Remove default images
  • Loading branch information
polldo authored Sep 20, 2023
1 parent 8ca7f6d commit ccd9e69
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 7 deletions.
8 changes: 8 additions & 0 deletions api/test/course_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (ct *courseTest) createCourseOK(t *testing.T) course.Course {
Name: "Test" + strconv.Itoa(rand.Intn(1000)),
Description: "This is a test course",
Price: rand.Intn(1000),
ImageURL: "/images/test.png",
}

body, err := json.Marshal(&c)
Expand Down Expand Up @@ -83,6 +84,7 @@ func (ct *courseTest) createCourseOK(t *testing.T) course.Course {
exp.Name = c.Name
exp.Description = c.Description
exp.Price = c.Price
exp.ImageURL = c.ImageURL

if diff := cmp.Diff(got, exp); diff != "" {
t.Fatalf("wrong course payload. Diff: \n%s", diff)
Expand All @@ -101,6 +103,7 @@ func (ct *courseTest) createCourseUnauth(t *testing.T) {
Name: "Test",
Description: "This is a test course",
Price: 100,
ImageURL: "/images/test.png",
}

body, err := json.Marshal(&c)
Expand Down Expand Up @@ -138,6 +141,7 @@ func (ct *courseTest) updateCourseOK(t *testing.T, crs course.Course) course.Cou
Name: ptr("Updated Test"),
Description: ptr("This is an updated test course"),
Price: ptr(500),
ImageURL: ptr("/images/updated.png"),
}

body, err := json.Marshal(&c)
Expand Down Expand Up @@ -169,6 +173,7 @@ func (ct *courseTest) updateCourseOK(t *testing.T, crs course.Course) course.Cou
exp.Name = *c.Name
exp.Description = *c.Description
exp.Price = *c.Price
exp.ImageURL = *c.ImageURL

if diff := cmp.Diff(got, exp); diff != "" {
t.Fatalf("wrong course payload. Diff: \n%s", diff)
Expand All @@ -194,6 +199,7 @@ func (ct *courseTest) updateCourseConcurrent(t *testing.T, crs course.Course) {
Name: ptr("Updated Test"),
Description: ptr("This is an updated test course"),
Price: ptr(500),
ImageURL: ptr("/images/updated.png"),
}

body, err := json.Marshal(&c)
Expand Down Expand Up @@ -243,6 +249,7 @@ func (ct *courseTest) updateCourseInexistent(t *testing.T, crs course.Course) {
Name: ptr("Updated Test Course Not Existent"),
Description: ptr("This is an updated test course - not exist"),
Price: ptr(300),
ImageURL: ptr("/images/updated.png"),
}

body, err := json.Marshal(&c)
Expand Down Expand Up @@ -276,6 +283,7 @@ func (ct *courseTest) updateCourseUnauth(t *testing.T, crs course.Course) {
Name: ptr("Updated Test Unauth"),
Description: ptr("This is an updated test course - unauth"),
Price: ptr(300),
ImageURL: ptr("/images/updated.png"),
}

body, err := json.Marshal(&c)
Expand Down
6 changes: 6 additions & 0 deletions api/test/video_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (vt *videoTest) createVideoOK(t *testing.T, course string, index int) video
Description: "This is a test video",
Free: true,
URL: "",
ImageURL: "/images/new.png",
}

body, err := json.Marshal(&v)
Expand Down Expand Up @@ -88,6 +89,7 @@ func (vt *videoTest) createVideoOK(t *testing.T, course string, index int) video
exp.Name = v.Name
exp.Description = v.Description
exp.Free = v.Free
exp.ImageURL = v.ImageURL

if diff := cmp.Diff(got, exp); diff != "" {
t.Fatalf("wrong video payload. Diff: \n%s", diff)
Expand All @@ -109,6 +111,7 @@ func (vt *videoTest) createVideoUnauth(t *testing.T, course course.Course) {
Description: "This is a test video",
Free: true,
URL: "",
ImageURL: "/images/new.png",
}

body, err := json.Marshal(&v)
Expand Down Expand Up @@ -145,6 +148,7 @@ func (vt *videoTest) createVideoIndexConflict(t *testing.T, course string, index
Description: "This is a test video",
Free: true,
URL: "",
ImageURL: "/images/new.png",
}

body, err := json.Marshal(&v)
Expand Down Expand Up @@ -180,6 +184,7 @@ func (vt *videoTest) updateVideoOK(t *testing.T, v video.Video) video.Video {
Name: ptr("Video Test" + strconv.Itoa(rand.Intn(1000))),
Description: ptr("This is a test video"),
Free: ptr(true),
ImageURL: ptr("/image/updated.png"),
}

body, err := json.Marshal(&vup)
Expand Down Expand Up @@ -212,6 +217,7 @@ func (vt *videoTest) updateVideoOK(t *testing.T, v video.Video) video.Video {
exp.Name = *vup.Name
exp.Description = *vup.Description
exp.Free = *vup.Free
exp.ImageURL = *vup.ImageURL

if diff := cmp.Diff(got, exp); diff != "" {
t.Fatalf("wrong video payload. Diff: \n%s", diff)
Expand Down
3 changes: 3 additions & 0 deletions core/course/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Course struct {
ID string `json:"id" db:"course_id"`
Name string `json:"name" db:"name"`
Description string `json:"description" db:"description"`
ImageURL string `json:"image_url" db:"image_url"`
Price int `json:"price" db:"price"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Expand All @@ -16,10 +17,12 @@ type CourseNew struct {
Name string `json:"name" validate:"required"`
Description string `json:"description" validate:"required"`
Price int `json:"price" validate:"required,gte=0,lte=10000"`
ImageURL string `json:"image_url" validate:"required"`
}

type CourseUp struct {
Name *string `json:"name"`
Description *string `json:"description"`
Price *int `json:"price" validate:"omitempty,gte=0,lte=10000"`
ImageURL *string `json:"image_url"`
}
4 changes: 4 additions & 0 deletions core/course/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func HandleCreate(db *sqlx.DB) web.Handler {
Name: c.Name,
Description: c.Description,
Price: c.Price,
ImageURL: c.ImageURL,
CreatedAt: now,
UpdatedAt: now,
}
Expand Down Expand Up @@ -86,6 +87,9 @@ func HandleUpdate(db *sqlx.DB) web.Handler {
if cup.Price != nil {
course.Price = *cup.Price
}
if cup.ImageURL != nil {
course.ImageURL = *cup.ImageURL
}
course.UpdatedAt = time.Now().UTC()

if course, err = Update(ctx, db, course); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions core/course/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
func Create(ctx context.Context, db sqlx.ExtContext, course Course) error {
const q = `
INSERT INTO courses
(course_id, name, description, price, created_at, updated_at)
(course_id, name, description, price, image_url, created_at, updated_at)
VALUES
(:course_id, :name, :description, :price, :created_at, :updated_at)`
(:course_id, :name, :description, :price, :image_url, :created_at, :updated_at)`

if err := database.NamedExecContext(ctx, db, q, course); err != nil {
return fmt.Errorf("inserting course: %w", err)
Expand All @@ -30,6 +30,7 @@ func Update(ctx context.Context, db sqlx.ExtContext, course Course) (Course, err
name = :name,
description = :description,
price = :price,
image_url = :image_url,
updated_at = :updated_at,
version = version + 1
WHERE
Expand Down
4 changes: 4 additions & 0 deletions core/video/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func HandleCreate(db *sqlx.DB) web.Handler {
Description: v.Description,
Free: v.Free,
URL: v.URL,
ImageURL: v.ImageURL,
CreatedAt: now,
UpdatedAt: now,
}
Expand Down Expand Up @@ -100,6 +101,9 @@ func HandleUpdate(db *sqlx.DB) web.Handler {
if vup.URL != nil {
video.URL = *vup.URL
}
if vup.ImageURL != nil {
video.ImageURL = *vup.ImageURL
}
video.UpdatedAt = time.Now().UTC()

if video, err = Update(ctx, db, video); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions core/video/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
func Create(ctx context.Context, db sqlx.ExtContext, video Video) error {
const q = `
INSERT INTO videos
(video_id, course_id, index, name, description, free, url, created_at, updated_at)
(video_id, course_id, index, name, description, free, url, image_url, created_at, updated_at)
VALUES
(:video_id, :course_id, :index, :name, :description, :free, :url, :created_at, :updated_at)`
(:video_id, :course_id, :index, :name, :description, :free, :url, :image_url, :created_at, :updated_at)`

if err := database.NamedExecContext(ctx, db, q, video); err != nil {
return fmt.Errorf("inserting video: %w", err)
Expand All @@ -32,6 +32,8 @@ func Update(ctx context.Context, db sqlx.ExtContext, video Video) (Video, error)
name = :name,
description = :description,
free = :free,
url = :url,
image_url = :image_url,
updated_at = :updated_at,
version = version + 1
WHERE
Expand Down
3 changes: 3 additions & 0 deletions core/video/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Video struct {
Description string `json:"description" db:"description"`
Free bool `json:"free" db:"free"`
URL string `json:"-" db:"url"`
ImageURL string `json:"image_url" db:"image_url"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Version int `json:"-" db:"version"`
Expand All @@ -27,6 +28,7 @@ type VideoNew struct {
Description string `json:"description" validate:"required"`
Free bool `json:"free" validate:"required"`
URL string `json:"url" validate:"omitempty,url"`
ImageURL string `json:"image_url" validate:"required"`
}

type VideoUp struct {
Expand All @@ -36,6 +38,7 @@ type VideoUp struct {
Description *string `json:"description"`
Free *bool `json:"free"`
URL *string `json:"url" validate:"omitempty,url"`
ImageURL *string `json:"image_url"`
}

type Progress struct {
Expand Down
1 change: 1 addition & 0 deletions database/sql/migration/000003_create_courses_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS courses
name TEXT NOT NULL,
description TEXT NOT NULL,
price INT NOT NULL,
image_url TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
version INT NOT NULL DEFAULT 1,
Expand Down
1 change: 1 addition & 0 deletions database/sql/migration/000004_create_videos_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS videos
description TEXT NOT NULL,
free BOOLEAN NOT NULL,
url TEXT NOT NULL,
image_url TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
version INT NOT NULL DEFAULT 1,
Expand Down
Binary file removed frontend/public/favicon.ico
Binary file not shown.
1 change: 0 additions & 1 deletion frontend/public/next.svg

This file was deleted.

Binary file removed frontend/public/random.jpeg
Binary file not shown.
1 change: 0 additions & 1 deletion frontend/public/thirteen.svg

This file was deleted.

1 change: 0 additions & 1 deletion frontend/public/vercel.svg

This file was deleted.

0 comments on commit ccd9e69

Please sign in to comment.