|
| 1 | +// Copyright 2022 The StudyGolang Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// https://studygolang.com |
| 5 | +// Author: polaris [email protected] |
| 6 | + |
| 7 | +package controller |
| 8 | + |
| 9 | +import ( |
| 10 | + "net/http" |
| 11 | + "strconv" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/studygolang/studygolang/context" |
| 15 | + . "github.com/studygolang/studygolang/http" |
| 16 | + "github.com/studygolang/studygolang/http/middleware" |
| 17 | + "github.com/studygolang/studygolang/logic" |
| 18 | + "github.com/studygolang/studygolang/model" |
| 19 | + |
| 20 | + echo "github.com/labstack/echo/v4" |
| 21 | +) |
| 22 | + |
| 23 | +// 在需要评论(喜欢)且要回调的地方注册评论(喜欢)对象 |
| 24 | +func init() { |
| 25 | + // 注册评论(喜欢)对象 |
| 26 | + logic.RegisterCommentObject(model.TypeInterview, logic.InterviewComment{}) |
| 27 | + logic.RegisterLikeObject(model.TypeInterview, logic.InterviewLike{}) |
| 28 | +} |
| 29 | + |
| 30 | +type InterviewController struct{} |
| 31 | + |
| 32 | +// RegisterRoute 注册路由 |
| 33 | +func (self InterviewController) RegisterRoute(g *echo.Group) { |
| 34 | + g.GET("/interview/question", self.TodayQuestion) |
| 35 | + g.GET("/interview/question/:show_sn", self.Find) |
| 36 | + |
| 37 | + g.Match([]string{"GET", "POST"}, "/interview/new", self.Create, middleware.NeedLogin(), middleware.AdminAuth()) |
| 38 | +} |
| 39 | + |
| 40 | +func (InterviewController) Create(ctx echo.Context) error { |
| 41 | + question := ctx.FormValue("question") |
| 42 | + // 请求新建面试题页面 |
| 43 | + if question == "" || ctx.Request().Method != "POST" { |
| 44 | + interview := &model.InterviewQuestion{} |
| 45 | + return render(ctx, "interview/new.html", map[string]interface{}{"interview": interview}) |
| 46 | + } |
| 47 | + |
| 48 | + forms, _ := ctx.FormParams() |
| 49 | + interview, err := logic.DefaultInterview.Publish(context.EchoContext(ctx), forms) |
| 50 | + if err != nil { |
| 51 | + return fail(ctx, 1, "内部服务错误!") |
| 52 | + } |
| 53 | + return success(ctx, interview) |
| 54 | +} |
| 55 | + |
| 56 | +// TodayQuestion 今日题目 |
| 57 | +func (ic InterviewController) TodayQuestion(ctx echo.Context) error { |
| 58 | + question := logic.DefaultInterview.TodayQuestion(context.EchoContext(ctx)) |
| 59 | + |
| 60 | + data := map[string]interface{}{ |
| 61 | + "title": "Go每日一题 今日(" + time.Now().Format("2006-01-02") + ")", |
| 62 | + } |
| 63 | + return ic.detail(ctx, question, data) |
| 64 | +} |
| 65 | + |
| 66 | +// Find 某个题目的详情 |
| 67 | +func (ic InterviewController) Find(ctx echo.Context) error { |
| 68 | + showSn := ctx.Param("show_sn") |
| 69 | + sn, err := strconv.ParseInt(showSn, 32, 64) |
| 70 | + if err != nil { |
| 71 | + return ctx.Redirect(http.StatusSeeOther, "/interview/question?"+err.Error()) |
| 72 | + } |
| 73 | + |
| 74 | + question, err := logic.DefaultInterview.FindOne(context.EchoContext(ctx), sn) |
| 75 | + if err != nil || question.Id == 0 { |
| 76 | + return ctx.Redirect(http.StatusSeeOther, "/interview/question") |
| 77 | + } |
| 78 | + |
| 79 | + data := map[string]interface{}{ |
| 80 | + "title": "Go每日一题(" + strconv.Itoa(question.Id) + ")", |
| 81 | + } |
| 82 | + |
| 83 | + return ic.detail(ctx, question, data) |
| 84 | +} |
| 85 | + |
| 86 | +func (InterviewController) detail(ctx echo.Context, question *model.InterviewQuestion, data map[string]interface{}) error { |
| 87 | + data["question"] = question |
| 88 | + me, ok := ctx.Get("user").(*model.Me) |
| 89 | + if ok { |
| 90 | + data["likeflag"] = logic.DefaultLike.HadLike(context.EchoContext(ctx), me.Uid, question.Id, model.TypeInterview) |
| 91 | + // data["hadcollect"] = logic.DefaultFavorite.HadFavorite(context.EchoContext(ctx), me.Uid, question.Id, model.TypeInterview) |
| 92 | + |
| 93 | + logic.Views.Incr(Request(ctx), model.TypeInterview, question.Id, me.Uid) |
| 94 | + |
| 95 | + go logic.DefaultViewRecord.Record(question.Id, model.TypeInterview, me.Uid) |
| 96 | + |
| 97 | + if me.IsRoot { |
| 98 | + data["view_user_num"] = logic.DefaultViewRecord.FindUserNum(context.EchoContext(ctx), question.Id, model.TypeInterview) |
| 99 | + data["view_source"] = logic.DefaultViewSource.FindOne(context.EchoContext(ctx), question.Id, model.TypeInterview) |
| 100 | + } |
| 101 | + } else { |
| 102 | + logic.Views.Incr(Request(ctx), model.TypeInterview, question.Id) |
| 103 | + } |
| 104 | + |
| 105 | + return render(ctx, "interview/question.html,common/comment.html", data) |
| 106 | +} |
0 commit comments