Skip to content

Commit

Permalink
feat: add global sort order for admins in helpful-links
Browse files Browse the repository at this point in the history
  • Loading branch information
CK-7vn committed Dec 4, 2024
1 parent 937268f commit e1a47fd
Show file tree
Hide file tree
Showing 27 changed files with 292 additions and 1,307 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
-- +goose Up
-- +goose StatementBegin
DROP TABLE IF EXISTS public.left_menu_links CASCADE;
CREATE TABLE public.helpful_links (
id SERIAL NOT NULL PRIMARY KEY,
created_at timestamp with time zone,
updated_at timestamp with time zone,
deleted_at timestamp with time zone,
visibility_status boolean DEFAULT true,
thumbnail_url CHARACTER VARYING(255) NOT NULL,
description CHARACTER VARYING(255) NOT NULL,
title CHARACTER VARYING(255) NOT NULL,
url CHARACTER VARYING(255) NOT NULL,
facility_id integer,
Expand All @@ -16,11 +20,35 @@ CREATE TABLE public.helpful_links (

CREATE INDEX idx_helpful_links_facility_id ON public.helpful_links USING btree (facility_id);
CREATE INDEX idx_helpful_links_title ON public.helpful_links USING btree (title);

INSERT INTO open_content_providers (
name,
base_url,
thumbnail,
currently_enabled,
description,
created_at,
updated_at
)
VALUES (
'HelpfulLinks',
'helpful_links',
'/ul-logo.png',
true,
'Helpful links for users!',
NOW(),
NOW()
);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS public.helpful_links CASCADE;
DROP TABLE IF EXISTS public.left_menu_links CASCADE;
CREATE TABLE public.left_menu_links (
id SERIAL NOT NULL PRIMARY KEY,
name character varying(255) NOT NULL,
rank integer DEFAULT 1,
links jsonb
);
-- +goose StatementEnd


19 changes: 18 additions & 1 deletion backend/src/database/DB.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,24 @@ func (db *DB) SeedDefaultData(isTesting bool) {
}
}

const defaultLeftMenuLinks = `[{"name":"Unlocked Labs","rank":1,"links":[{"Unlocked Labs Website":"http:\/\/www.unlockedlabs.org\/"},{"Unlocked Labs LinkedIn":"https:\/\/www.linkedin.com\/company\/labs-unlocked\/"}],"created_at":null,"updated_at":null}]`
const defaultLeftMenuLinks = `[{
"title": "Unlocked Labs",
"description": "Unlocked Labs website",
"url": "https://unlockedlabs.org",
"visibility_status": true,
"thumbnail_url": "https://unlockedlabs.org/favicon.ico",
"open_content_provider_id": 1,
"facility_id": 1
},
{
"title": "Google",
"description": "Google search engine",
"url": "https://www.google.com",
"visibility_status": true,
"open_content_provider_id": 1,
"thumbnail_url": "https://www.google.com/favicon.ico",
"facility_id": 1
}]`

func (db *DB) SeedTestData() {
facilitiesFile, err := os.ReadFile("test_data/facilities.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (db *DB) GetHelpfulLinks(page, perPage int, search, orderBy string) (int64, []models.HelpfulLink, error) {
var links []models.HelpfulLink
links := make([]models.HelpfulLink, 0, perPage)
tx := db.Model(&models.HelpfulLink{})
var total int64

Expand All @@ -33,6 +33,7 @@ func (db *DB) GetHelpfulLinks(page, perPage int, search, orderBy string) (int64,
}

offset := (page - 1) * perPage
//Pull and add calcOffset
if err := tx.Offset(offset).Limit(perPage).Find(&links).Error; err != nil {
return 0, nil, newGetRecordsDBError(err, "helpful_links")
}
Expand Down Expand Up @@ -78,3 +79,19 @@ func (db *DB) ToggleVisibilityStatus(id int) error {
}
return nil
}

func (db *DB) GetLinkFromId(id uint) (*models.HelpfulLink, error) {
var link models.HelpfulLink
if err := db.Model(&models.HelpfulLink{}).Where("id = ?", id).First(&link).Error; err != nil {
return nil, newGetRecordsDBError(err, "helpful_links")
}
return &link, nil
}

func (db *DB) GetHelpfulLinkOpenContentProviderId() (uint, error) {
var provider models.OpenContentProvider
if err := db.Model(&models.OpenContentProvider{}).Where("name = ?", "HelpfulLinks").First(&provider).Error; err != nil {
return 0, newGetRecordsDBError(err, "open_content_providers")
}
return provider.ID, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,42 @@ func (srv *Server) registerLeftMenuRoutes() []routeDef {
{"PATCH /api/helpful-links/{id}/edit", srv.handleEditLink, true, axx},
{"PUT /api/helpful-links/toggle/{id}", srv.handleToggleVisibilityStatus, true, axx},
{"DELETE /api/helpful-links/{id}", srv.handleDeleteLink, true, axx},
{"PUT /api/helpful-links/activity/{id}", srv.handleAddUserActivity, false, axx},
{"PUT /api/helpful-links/sort", srv.changeSortOrder, true, axx},
}
}

var HelpfulSortOrder = make(map[uint]string)

func (srv *Server) changeSortOrder(w http.ResponseWriter, r *http.Request, log sLog) error {
type reqBody struct {
SortOrder string `json:"sort_order"`
}
var req reqBody
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return newJSONReqBodyServiceError(err)
}
defer r.Body.Close()
facilityID := srv.getFacilityID(r)
HelpfulSortOrder[facilityID] = req.SortOrder
return writeJsonResponse(w, http.StatusOK, "Sort order changed successfully")
}

func (srv *Server) handleGetHelpfulLinks(w http.ResponseWriter, r *http.Request, log sLog) error {
search := r.URL.Query().Get("search")
orderBy := r.URL.Query().Get("order_by")
page, perPage := srv.getPaginationInfo(r)
total, links, err := srv.Db.GetHelpfulLinks(page, perPage, search, orderBy)
total, links, err := srv.Db.GetHelpfulLinks(page, perPage, search, HelpfulSortOrder[srv.getFacilityID(r)])
if err != nil {
return newInternalServerServiceError(err, "error fetching helpful links")
}
meta := models.NewPaginationInfo(page, perPage, total)
return writePaginatedResponse(w, http.StatusOK, links, meta)
respSort := struct {
SortOrder string `json:"sort_order"`
Links []models.HelpfulLink `json:"helpful_links"`
Meta models.PaginationMeta `json:"meta"`
}{SortOrder: HelpfulSortOrder[srv.getFacilityID(r)], Links: links, Meta: meta}

return writeJsonResponse(w, http.StatusOK, respSort)
}

func (srv *Server) handleAddHelpfulLink(w http.ResponseWriter, r *http.Request, log sLog) error {
Expand Down Expand Up @@ -82,3 +105,30 @@ func (srv *Server) handleDeleteLink(w http.ResponseWriter, r *http.Request, log
}
return writeJsonResponse(w, http.StatusOK, "Link deleted successfully")
}

func (srv *Server) handleAddUserActivity(w http.ResponseWriter, r *http.Request, log sLog) error {
var activity models.OpenContentActivity
userID := srv.getUserID(r)
facilityID := srv.getFacilityID(r)
linkID, err := strconv.Atoi(r.PathValue("id"))
if err != nil {
return newInvalidIdServiceError(err, "Invalid id")
}
link, err := srv.Db.GetLinkFromId(uint(linkID))
if err != nil {
return newDatabaseServiceError(err)
}
openContentProviderID, err := srv.Db.GetHelpfulLinkOpenContentProviderId()
if err != nil {
return newDatabaseServiceError(err)
}

activity.UserID = userID
activity.FacilityID = facilityID
activity.ContentID = link.ID
activity.OpenContentProviderID = openContentProviderID
srv.Db.CreateContentActivity(link.Url, &activity)
return writeJsonResponse(w, http.StatusOK, map[string]string{
"url": link.Url,
})
}
97 changes: 0 additions & 97 deletions backend/src/handlers/left_menu_handler_test.go

This file was deleted.

15 changes: 15 additions & 0 deletions backend/src/models/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package models

import (
"gorm.io/gorm"
)

type HelpfulLink struct {
DatabaseFields
Title string `gorm:"size:255;not null" json:"title"`
Description string `gorm:"size:255;not null" json:"description"`
Url string `gorm:"size:255;not null" json:"url"`
VisibilityStatus bool `gorm:"default:true" json:"visibility_status"`
ThumbnailUrl string `gorm:"size:255" json:"thumbnail_url"`
OpenContentProviderID uint `json:"open_content_provider_id"`
FacilityID uint `json:"facility_id"`
}

func (HelpfulLink) TableName() string {
return "helpful_links"
}
func (hl *HelpfulLink) BeforeCreate(tx *gorm.DB) error {
var id int
if hl.OpenContentProviderID == 0 {
if err := tx.Table("open_content_providers").Select("id").Where("name = ? ", HelpfulLinks).Scan(&id).Error; err != nil {
return err
}
hl.OpenContentProviderID = uint(id)
}
return nil
}

type ProgramFavorite struct {
ID uint `gorm:"primaryKey" json:"-"`
Expand Down
24 changes: 14 additions & 10 deletions backend/src/models/open_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,20 @@ type OpenContentItem struct {
}

const (
KolibriThumbnailUrl string = "https://learningequality.org/static/assets/kolibri-ecosystem-logos/blob-logo.svg"
Kiwix string = "Kiwix"
KolibriDescription string = "Kolibri provides an extensive library of educational content suitable for all learning levels."
KiwixThumbnailURL string = "/kiwix.jpg"
KiwixDescription string = "Kiwix is an offline reader that allows you to host a wide array of educational content."
KiwixLibraryUrl string = "https://library.kiwix.org"
YoutubeThumbnail string = "/youtube.png"
Youtube string = "Youtube"
YoutubeApi string = "https://www.googleapis.com/youtube/v3/videos"
YoutubeDescription string = "Hand pick videos to be available to students from youtube URL's"
KolibriThumbnailUrl string = "https://learningequality.org/static/assets/kolibri-ecosystem-logos/blob-logo.svg"
Kiwix string = "Kiwix"
KolibriDescription string = "Kolibri provides an extensive library of educational content suitable for all learning levels."
KiwixThumbnailURL string = "/kiwix.jpg"
KiwixDescription string = "Kiwix is an offline reader that allows you to host a wide array of educational content."
KiwixLibraryUrl string = "https://library.kiwix.org"
YoutubeThumbnail string = "/youtube.png"
Youtube string = "Youtube"
YoutubeApi string = "https://www.googleapis.com/youtube/v3/videos"
YoutubeDescription string = "Hand pick videos to be available to students from youtube URL's"
HelpfulLinks string = "HelpfulLinks"
HelpfulLinksThumbnail string = "/ul-logo.png"
HelpfulLinksUrl string = ""
HelpfulLinksDescription string = "Hand picked helpful links for users"
)

func (cp *OpenContentProvider) BeforeCreate(tx *gorm.DB) error {
Expand Down
42 changes: 0 additions & 42 deletions frontend/src/Components/LinkItem.tsx

This file was deleted.

Loading

0 comments on commit e1a47fd

Please sign in to comment.