show.go
1 package feeds 2 3 import ( 4 "context" 5 6 "github.com/google/uuid" 7 8 "github.com/gofiber/fiber/v2" 9 "github.com/mrusme/journalist/ent/feed" 10 11 // "github.com/mrusme/journalist/ent" 12 "go.uber.org/zap" 13 ) 14 15 type FeedShowResponse struct { 16 Success bool `json:"success"` 17 Feed *FeedShowModel `json:"feed"` 18 Message string `json:"message"` 19 } 20 21 // Show godoc 22 // @Summary Show a feed 23 // @Description Get feed by ID 24 // @Tags feeds 25 // @Accept json 26 // @Produce json 27 // @Param id path string true "Feed ID" 28 // @Success 200 {object} FeedShowResponse 29 // @Failure 400 {object} FeedShowResponse 30 // @Failure 404 {object} FeedShowResponse 31 // @Failure 500 {object} FeedShowResponse 32 // @Router /feeds/{id} [get] 33 // @security BasicAuth 34 func (h *handler) Show(ctx *fiber.Ctx) error { 35 var err error 36 37 param_id := ctx.Params("id") 38 id, err := uuid.Parse(param_id) 39 if err != nil { 40 h.logger.Debug( 41 "Could not parse user ID", 42 zap.Error(err), 43 ) 44 return ctx. 45 Status(fiber.StatusBadRequest). 46 JSON(FeedShowResponse{ 47 Success: false, 48 Feed: nil, 49 Message: err.Error(), 50 }) 51 } 52 53 dbFeed, err := h.entClient.Feed. 54 Query(). 55 Where( 56 feed.ID(id), 57 ). 58 Only(context.Background()) 59 if err != nil { 60 h.logger.Debug( 61 "Could not query feed", 62 zap.String("feedID", param_id), 63 zap.Error(err), 64 ) 65 return ctx. 66 Status(fiber.StatusInternalServerError). 67 JSON(FeedShowResponse{ 68 Success: false, 69 Feed: nil, 70 Message: err.Error(), 71 }) 72 } 73 74 // TODO: Check if user is subscribed to feed. Unless it's an admin, the should 75 // not be able to query feeds that they're not subscribed to. 76 // role := ctx.Locals("role").(string) 77 // if param_id != feed_id && role != "admin" { 78 // h.logger.Debug( 79 // "User not allowed to see other feeds", 80 // zap.Error(err), 81 // ) 82 // return ctx. 83 // Status(fiber.StatusForbidden). 84 // JSON(FeedShowResponse{ 85 // Success: false, 86 // Feed: nil, 87 // Message: "Only admins are allowed to see other feeds", 88 // }) 89 // } 90 91 showFeed := FeedShowModel{ 92 ID: dbFeed.ID.String(), 93 Name: dbFeed.FeedTitle, 94 URL: dbFeed.FeedFeedLink, 95 Group: "*", 96 } 97 98 return ctx. 99 Status(fiber.StatusOK). 100 JSON(FeedShowResponse{ 101 Success: true, 102 Feed: &showFeed, 103 Message: "", 104 }) 105 }