Skip to content

Commit

Permalink
Merge fe27bb6 into e7c841e
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Aug 19, 2018
2 parents e7c841e + fe27bb6 commit e8e108b
Show file tree
Hide file tree
Showing 26 changed files with 475 additions and 488 deletions.
14 changes: 1 addition & 13 deletions controller/app/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (c *ctrl) courseView(ctx *hime.Context) error {
return err
}
}

x, err := c.Repository.GetCourse(ctx, id)
if err == entity.ErrNotFound {
return share.NotFound(ctx)
Expand Down Expand Up @@ -73,14 +74,6 @@ func (c *ctrl) courseView(ctx *hime.Context) error {
owned = user.ID == x.UserID
}

// if user enrolled or user is owner fetch course contents
if enrolled || owned {
x.Contents, err = c.Repository.GetCourseContents(ctx, x.ID)
if err != nil {
return err
}
}

if owned {
x.Owner = user
} else {
Expand Down Expand Up @@ -146,11 +139,6 @@ func (c *ctrl) courseContent(ctx *hime.Context) error {
return err
}

x.Owner, err = c.Repository.GetUser(ctx, x.UserID)
if err != nil {
return err
}

var content *entity.CourseContent
pg, _ := strconv.Atoi(ctx.FormValue("p"))
if pg < 0 {
Expand Down
83 changes: 83 additions & 0 deletions controller/app/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package app

import (
"time"

"github.com/acoshift/acourse/entity"
)

// PublicCourse type
type PublicCourse struct {
ID string
Option entity.CourseOption
Title string
Desc string
Image string
Start time.Time
URL string
Type int
Price float64
Discount float64
}

// Link returns course link
func (x *PublicCourse) Link() string {
if x.URL != "" {
return x.URL
}
return x.ID
}

// ShowStart returns true if course should show start date
func (x *PublicCourse) ShowStart() bool {
return x.Type == entity.Live && !x.Start.IsZero()
}

// EnrolledCourse type
type EnrolledCourse struct {
ID string
Title string
Desc string
Image string
Start time.Time
URL string
Type int
}

// Link returns course link
func (x *EnrolledCourse) Link() string {
if x.URL != "" {
return x.URL
}
return x.ID
}

// ShowStart returns true if course should show start date
func (x *EnrolledCourse) ShowStart() bool {
return x.Type == entity.Live && !x.Start.IsZero()
}

// OwnCourse type
type OwnCourse struct {
ID string
Title string
Desc string
Image string
Start time.Time
URL string
Type int
EnrollCount int
}

// Link returns course link
func (x *OwnCourse) Link() string {
if x.URL != "" {
return x.URL
}
return x.ID
}

// ShowStart returns true if course should show start date
func (x *OwnCourse) ShowStart() bool {
return x.Type == entity.Live && !x.Start.IsZero()
}
6 changes: 3 additions & 3 deletions controller/app/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Repository interface {
GetCourseContents(ctx context.Context, courseID string) ([]*entity.CourseContent, error)
GetUser(ctx context.Context, userID string) (*entity.User, error)
FindAssignmentsByCourseID(ctx context.Context, courseID string) ([]*entity.Assignment, error)
ListPublicCourses(ctx context.Context) ([]*entity.Course, error)
ListOwnCourses(ctx context.Context, userID string) ([]*entity.Course, error)
ListEnrolledCourses(ctx context.Context, userID string) ([]*entity.Course, error)
ListPublicCourses(ctx context.Context) ([]*PublicCourse, error)
ListOwnCourses(ctx context.Context, userID string) ([]*OwnCourse, error)
ListEnrolledCourses(ctx context.Context, userID string) ([]*EnrolledCourse, error)
}
29 changes: 16 additions & 13 deletions controller/editor/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
"github.com/acoshift/acourse/context/appctx"
"github.com/acoshift/acourse/controller/share"
"github.com/acoshift/acourse/entity"
"github.com/acoshift/acourse/repository"
"github.com/acoshift/acourse/service"
"github.com/acoshift/acourse/view"
)

func (c *ctrl) contentList(ctx *hime.Context) error {
id := ctx.FormValue("id")

course, err := repository.GetCourse(ctx, id)
course, err := c.Repository.GetCourse(ctx, id)
if err == entity.ErrNotFound {
return share.NotFound(ctx)
}
if err != nil {
return err
}
course.Contents, err = repository.GetCourseContents(ctx, id)
course.Contents, err = c.Service.ListCourseContents(ctx, id)
if err != nil {
return err
}
Expand All @@ -33,12 +33,14 @@ func (c *ctrl) contentList(ctx *hime.Context) error {
}

func (c *ctrl) postContentList(ctx *hime.Context) error {
id := ctx.FormValue("id")

if ctx.FormValue("action") == "delete" {
contentID := ctx.FormValue("contentId")

err := repository.DeleteCourseContent(ctx, id, contentID)
err := c.Service.DeleteCourseContent(ctx, contentID)
if service.IsUIError(err) {
// TODO: use flash
return ctx.Status(http.StatusBadRequest).Error(err.Error())
}
if err != nil {
return err
}
Expand All @@ -49,7 +51,7 @@ func (c *ctrl) postContentList(ctx *hime.Context) error {
func (c *ctrl) contentCreate(ctx *hime.Context) error {
id := ctx.FormValue("id")

course, err := repository.GetCourse(ctx, id)
course, err := c.Repository.GetCourse(ctx, id)
if err != nil {
return err
}
Expand All @@ -68,7 +70,7 @@ func (c *ctrl) postContentCreate(ctx *hime.Context) error {
videoID = ctx.FormValue("videoId")
)

_, err := repository.RegisterCourseContent(ctx, &entity.RegisterCourseContent{
_, err := c.Service.CreateCourseContent(ctx, &entity.RegisterCourseContent{
CourseID: id,
Title: title,
LongDesc: desc,
Expand All @@ -86,15 +88,15 @@ func (c *ctrl) contentEdit(ctx *hime.Context) error {
// course content id
id := ctx.FormValue("id")

content, err := repository.GetCourseContent(ctx, id)
content, err := c.Service.GetCourseContent(ctx, id)
if err == entity.ErrNotFound {
return share.NotFound(ctx)
}
if err != nil {
return err
}

course, err := repository.GetCourse(ctx, content.CourseID)
course, err := c.Repository.GetCourse(ctx, content.CourseID)
if err != nil {
return err
}
Expand All @@ -115,21 +117,22 @@ func (c *ctrl) postContentEdit(ctx *hime.Context) error {
// course content id
id := ctx.FormValue("id")

content, err := repository.GetCourseContent(ctx, id)
content, err := c.Service.GetCourseContent(ctx, id)
if err == entity.ErrNotFound {
return share.NotFound(ctx)
}
if err != nil {
return err
}

course, err := repository.GetCourse(ctx, content.CourseID)
course, err := c.Repository.GetCourse(ctx, content.CourseID)
if err != nil {
return err
}

user := appctx.GetUser(ctx)
// user is not course owner
// TODO: move to service
if user.ID != course.UserID {
return ctx.Status(http.StatusForbidden).StatusText()
}
Expand All @@ -140,7 +143,7 @@ func (c *ctrl) postContentEdit(ctx *hime.Context) error {
videoID = ctx.FormValue("videoId")
)

err = repository.UpdateCourseContent(ctx, course.ID, id, title, desc, videoID)
err = c.Service.UpdateCourseContent(ctx, id, title, desc, videoID)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions controller/editor/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/acoshift/hime"

"github.com/acoshift/acourse/context/appctx"
"github.com/acoshift/acourse/repository"
"github.com/acoshift/acourse/service"
"github.com/acoshift/acourse/view"
)
Expand Down Expand Up @@ -51,7 +50,7 @@ func (c *ctrl) postCourseCreate(ctx *hime.Context) error {
return err
}

link, _ := repository.GetCourseURL(ctx, courseID)
link, _ := c.Repository.GetCourseURL(ctx, courseID)
if link == "" {
return ctx.RedirectTo("app.course", courseID)
}
Expand All @@ -60,7 +59,7 @@ func (c *ctrl) postCourseCreate(ctx *hime.Context) error {

func (c *ctrl) courseEdit(ctx *hime.Context) error {
id := ctx.FormValue("id")
course, err := repository.GetCourse(ctx, id)
course, err := c.Repository.GetCourse(ctx, id)
if err != nil {
return err
}
Expand Down Expand Up @@ -109,7 +108,7 @@ func (c *ctrl) postCourseEdit(ctx *hime.Context) error {
return err
}

link, _ := repository.GetCourseURL(ctx, id)
link, _ := c.Repository.GetCourseURL(ctx, id)
if link == "" {
return ctx.RedirectTo("app.course", id)
}
Expand Down
11 changes: 6 additions & 5 deletions controller/editor/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,28 @@ import (

// Config is editor config
type Config struct {
Service service.Service
Service service.Service
Repository Repository
}

// New creates new editor handler
func New(cfg Config) http.Handler {
c := &ctrl{cfg}

mux := http.NewServeMux()
mux.Handle("/course/create", onlyInstructor(methodmux.GetPost(
mux.Handle("/course/create", c.onlyInstructor(methodmux.GetPost(
hime.Handler(c.courseCreate),
hime.Handler(c.postCourseCreate),
)))
mux.Handle("/course/edit", isCourseOwner(methodmux.GetPost(
mux.Handle("/course/edit", c.isCourseOwner(methodmux.GetPost(
hime.Handler(c.courseEdit),
hime.Handler(c.postCourseEdit),
)))
mux.Handle("/content", isCourseOwner(methodmux.GetPost(
mux.Handle("/content", c.isCourseOwner(methodmux.GetPost(
hime.Handler(c.contentList),
hime.Handler(c.postContentList),
)))
mux.Handle("/content/create", isCourseOwner(methodmux.GetPost(
mux.Handle("/content/create", c.isCourseOwner(methodmux.GetPost(
hime.Handler(c.contentCreate),
hime.Handler(c.postContentCreate),
)))
Expand Down
7 changes: 3 additions & 4 deletions controller/editor/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"github.com/acoshift/acourse/context/appctx"
"github.com/acoshift/acourse/controller/share"
"github.com/acoshift/acourse/entity"
"github.com/acoshift/acourse/repository"
)

func onlyInstructor(h http.Handler) http.Handler {
func (c *ctrl) onlyInstructor(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u := appctx.GetUser(r.Context())
if u == nil {
Expand All @@ -26,7 +25,7 @@ func onlyInstructor(h http.Handler) http.Handler {
})
}

func isCourseOwner(h http.Handler) http.Handler {
func (c *ctrl) isCourseOwner(h http.Handler) http.Handler {
return hime.Handler(func(ctx *hime.Context) error {
u := appctx.GetUser(ctx)
if u == nil {
Expand All @@ -35,7 +34,7 @@ func isCourseOwner(h http.Handler) http.Handler {

id := ctx.FormValue("id")

ownerID, err := repository.GetCourseUserID(ctx, id)
ownerID, err := c.Repository.GetCourseUserID(ctx, id)
if err == entity.ErrNotFound {
return share.NotFound(ctx)
}
Expand Down
14 changes: 14 additions & 0 deletions controller/editor/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package editor

import (
"context"

"github.com/acoshift/acourse/entity"
)

// Repository is editor storage
type Repository interface {
GetCourse(ctx context.Context, courseID string) (*entity.Course, error)
GetCourseUserID(ctx context.Context, courseID string) (string, error)
GetCourseURL(ctx context.Context, courseID string) (string, error)
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func main() {
m.Handle("/editor/", http.StripPrefix("/editor", middleware.Chain(
// -
)(editor.New(editor.Config{
Service: svc,
Service: svc,
Repository: repository.NewEditor(),
}))))

m.Handle("/admin/", http.StripPrefix("/admin", middleware.Chain(
Expand Down
Loading

0 comments on commit e8e108b

Please sign in to comment.