Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPTUBE-76 Adapt queries to new database collections #77

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go-server/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/api/option"
)

// RELATIONS MODEL: https://excalidraw.com/#json=El2YZ_1cylEftfyJ0op57,kWn7gvhTzWNN__wxXBJcHA
const (
USERS_COLLECTION = "Users"
// Billing collection names
Expand Down
82 changes: 31 additions & 51 deletions go-server/database/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@ import (
"google.golang.org/api/youtube/v3"
)

const (
usersCollName = "users"
youtubeCollName = "youtube"
negativeCommentsCollName = "NegativeComments"
)

func GetYoutubeResult(emailUser string, videoId string) (*models.YoutubeVideoAnalyzed, error) {
func GetYoutubeAnalysisResult(userId string, videoId string) (*models.YoutubeVideoAnalyzed, error) {
client, err := GetClient()
if err != nil {
return nil, err
}
defer client.Close()
snap, err := client.Collection("users").
Doc(emailUser).Collection("youtube").Doc(videoId).Get(Ctx)
snap, err := client.Collection(YOUTUBE_ANALYSES_COLLECTION).
Doc(userId).Collection(YOUTUBE_VIDEOS_COLLECTION).Doc(videoId).Get(Ctx)
if err != nil {
return nil, err
}
Expand All @@ -38,21 +32,6 @@ func GetYoutubeResult(emailUser string, videoId string) (*models.YoutubeVideoAna
return &result, nil
}

func GetYoutubeLandingResult(videoID string) (*models.YoutubeAnalyzerLandingRespBody, error) {
client, err := GetClient()
if err != nil {
return nil, err
}
defer client.Close()
snap, err := client.Collection("landing").Doc(videoID).Get(Ctx)
if err != nil {
return nil, err
}
var result models.YoutubeAnalyzerLandingRespBody
snap.DataTo(&result)
return &result, nil
}

func AddYoutubeResult(analysis *models.YoutubeAnalyzerRespBody) error {
client, err := GetClient()
if err != nil {
Expand All @@ -61,40 +40,41 @@ func AddYoutubeResult(analysis *models.YoutubeAnalyzerRespBody) error {
defer client.Close()

currentTime := time.Now().UTC()
existingResult, err := GetYoutubeResult(analysis.AccountEmail, analysis.VideoId)
existingResult, err := GetYoutubeAnalysisResult(analysis.UserId, analysis.VideoId)
if err != nil {
analysis.VideoResults.CreatedAt = currentTime
} else {
analysis.VideoResults.CreatedAt = existingResult.CreatedAt
}
analysis.VideoResults.LastUpdate = currentTime

userDoc := client.Collection("users").Doc(analysis.AccountEmail)
analysisDoc := client.Collection(YOUTUBE_ANALYSES_COLLECTION).Doc(analysis.UserId)
if existingResult != nil {
_, err = userDoc.Update(Ctx, []firestore.Update{
_, err = analysisDoc.Update(Ctx, []firestore.Update{
{
Path: "usageLimitYoutube",
Path: "usage_count",
Value: firestore.Increment(1),
},
})
} else {
_, err = userDoc.Set(Ctx, map[string]interface{}{
"email": analysis.AccountEmail,
"usageLimitYoutube": 1,
})
initialAnalysisDoc := models.YoutubeAnalysis{
UserId: analysis.UserId,
UsageCount: 1,
}
_, err = analysisDoc.Set(Ctx, initialAnalysisDoc)
}

if err != nil {
return err
}

youtubeDoc := userDoc.Collection("youtube").Doc(analysis.VideoId)
_, err = youtubeDoc.Set(Ctx, analysis.VideoResults)
youtubeVideoDoc := analysisDoc.Collection(YOUTUBE_VIDEOS_COLLECTION).Doc(analysis.VideoId)
_, err = youtubeVideoDoc.Set(Ctx, analysis.VideoResults)
if err != nil {
return err
}

negativeCommentsColl := youtubeDoc.Collection("NegativeComments")
negativeCommentsColl := youtubeVideoDoc.Collection(YOUTUBE_NEGATIVE_COMMENTS_COLLECTION)
for _, comment := range analysis.VideoResults.NegativeComments {
_, err = negativeCommentsColl.Doc(comment.Id).Get(Ctx)
if err != nil {
Expand All @@ -112,7 +92,7 @@ func AddYoutubeResult(analysis *models.YoutubeAnalyzerRespBody) error {
return nil
}

func GetYoutubeVideosPage(page int, pageSize int, accountEmail string) (*models.YoutubeVideosRespBody, error) {
func GetYoutubeVideosPage(page int, pageSize int, userId string) (*models.YoutubeVideosRespBody, error) {
client, err := GetClient()
if err != nil {
return nil, err
Expand All @@ -126,10 +106,10 @@ func GetYoutubeVideosPage(page int, pageSize int, accountEmail string) (*models.
Results: make([]models.YoutubeVideoDashboard, 0),
}

userDoc := client.Collection("users").Doc(accountEmail)
youtubeColl := userDoc.Collection("youtube")
analyzesDoc := client.Collection(YOUTUBE_ANALYSES_COLLECTION).Doc(userId)
videosColl := analyzesDoc.Collection(YOUTUBE_VIDEOS_COLLECTION)

pageVideos, totalVideos, err := GetPageFromCollection(page, pageSize, youtubeColl, "last_update", firestore.Desc)
pageVideos, totalVideos, err := GetPageFromCollection(page, pageSize, videosColl, "last_update", firestore.Desc)

if err != nil {
return nil, err
Expand All @@ -141,10 +121,10 @@ func GetYoutubeVideosPage(page int, pageSize int, accountEmail string) (*models.
// Filling the next and previous fields
totalPages := int(math.Ceil(float64(response.Count) / float64(pageSize)))
baseURL := config.Config("APP_BASE_URL")
prevPageVal := fmt.Sprintf("%s/api/youtube/videos?account_email=%s&page=%v&page_size=%v",
baseURL, accountEmail, page-1, pageSize)
nextPageVal := fmt.Sprintf("%s/api/youtube/videos?account_email=%s&page=%v&page_size=%v",
baseURL, accountEmail, page+1, pageSize)
prevPageVal := fmt.Sprintf("%s/api/youtube/videos?user_id=%s&page=%v&page_size=%v",
baseURL, userId, page-1, pageSize)
nextPageVal := fmt.Sprintf("%s/api/youtube/videos?user_id=%s&page=%v&page_size=%v",
baseURL, userId, page+1, pageSize)

if 1 <= page && page <= totalPages {
response.Previous = &prevPageVal
Expand All @@ -169,7 +149,7 @@ func GetYoutubeVideosPage(page int, pageSize int, accountEmail string) (*models.
return response, nil
}

func GetNegativeCommentsPage(page int, pageSize int, accountEmail string, videoId string) (*models.YoutubeVideoNegativeCommentsRespBody, error) {
func GetNegativeCommentsPage(page int, pageSize int, userId string, videoId string) (*models.YoutubeVideoNegativeCommentsRespBody, error) {
client, err := GetClient()
if err != nil {
return nil, err
Expand All @@ -183,9 +163,9 @@ func GetNegativeCommentsPage(page int, pageSize int, accountEmail string, videoI
Results: make([]*youtube.Comment, 0),
}

userDoc := client.Collection(usersCollName).Doc(accountEmail)
videoDoc := userDoc.Collection(youtubeCollName).Doc(videoId)
negativeCommentsColl := videoDoc.Collection(negativeCommentsCollName)
analysisDoc := client.Collection(YOUTUBE_ANALYSES_COLLECTION).Doc(userId)
videoDoc := analysisDoc.Collection(YOUTUBE_VIDEOS_COLLECTION).Doc(videoId)
negativeCommentsColl := videoDoc.Collection(YOUTUBE_NEGATIVE_COMMENTS_COLLECTION)

pageComments, totalComments, err := GetPageFromCollection(page, pageSize, negativeCommentsColl, "", firestore.Asc)

Expand All @@ -199,10 +179,10 @@ func GetNegativeCommentsPage(page int, pageSize int, accountEmail string, videoI
// Filling the next and previous fields
totalPages := int(math.Ceil(float64(response.Count) / float64(pageSize)))
baseURL := config.Config("APP_BASE_URL")
prevPageVal := fmt.Sprintf("%s/api/youtube/videos/%s/negative-comments?account_email=%s&page=%v&page_size=%v",
baseURL, videoId, accountEmail, page-1, pageSize)
nextPageVal := fmt.Sprintf("%s/api/youtube/videos/%s/negative-comments?account_email=%s&page=%v&page_size=%v",
baseURL, videoId, accountEmail, page+1, pageSize)
prevPageVal := fmt.Sprintf("%s/api/youtube/videos/%s/negative-comments?user_id=%s&page=%v&page_size=%v",
baseURL, videoId, userId, page-1, pageSize)
nextPageVal := fmt.Sprintf("%s/api/youtube/videos/%s/negative-comments?user_id=%s&page=%v&page_size=%v",
baseURL, videoId, userId, page+1, pageSize)

if 1 <= page && page <= totalPages {
response.Previous = &prevPageVal
Expand Down
25 changes: 13 additions & 12 deletions go-server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ const docTemplate = `{
"parameters": [
{
"type": "string",
"description": "the account email",
"name": "account_email",
"description": "the user id",
"name": "user_id",
"in": "query",
"required": true
},
Expand Down Expand Up @@ -239,8 +239,8 @@ const docTemplate = `{
},
{
"type": "string",
"description": "the account email",
"name": "account_email",
"description": "the user id",
"name": "user_id",
"in": "query",
"required": true
}
Expand Down Expand Up @@ -284,8 +284,8 @@ const docTemplate = `{
},
{
"type": "string",
"description": "the account email",
"name": "account_email",
"description": "the user_id",
"name": "user_id",
"in": "query",
"required": true
},
Expand Down Expand Up @@ -1473,26 +1473,27 @@ const docTemplate = `{
"models.YoutubeAnalyzerReqBody": {
"type": "object",
"properties": {
"account_email": {
"description": "The email of the account sending the request",
"email": {
"description": "the email to send the results",
"type": "string"
},
"email": {
"description": "The email that will be used to send the results",
"user_id": {
"description": "The account user id",
"type": "string"
},
"video_id": {
"description": "the video id to analyze",
"type": "string"
}
}
},
"models.YoutubeAnalyzerRespBody": {
"type": "object",
"properties": {
"account_email": {
"email": {
"type": "string"
},
"email": {
"user_id": {
"type": "string"
},
"video_id": {
Expand Down
25 changes: 13 additions & 12 deletions go-server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@
"parameters": [
{
"type": "string",
"description": "the account email",
"name": "account_email",
"description": "the user id",
"name": "user_id",
"in": "query",
"required": true
},
Expand Down Expand Up @@ -232,8 +232,8 @@
},
{
"type": "string",
"description": "the account email",
"name": "account_email",
"description": "the user id",
"name": "user_id",
"in": "query",
"required": true
}
Expand Down Expand Up @@ -277,8 +277,8 @@
},
{
"type": "string",
"description": "the account email",
"name": "account_email",
"description": "the user_id",
"name": "user_id",
"in": "query",
"required": true
},
Expand Down Expand Up @@ -1466,26 +1466,27 @@
"models.YoutubeAnalyzerReqBody": {
"type": "object",
"properties": {
"account_email": {
"description": "The email of the account sending the request",
"email": {
"description": "the email to send the results",
"type": "string"
},
"email": {
"description": "The email that will be used to send the results",
"user_id": {
"description": "The account user id",
"type": "string"
},
"video_id": {
"description": "the video id to analyze",
"type": "string"
}
}
},
"models.YoutubeAnalyzerRespBody": {
"type": "object",
"properties": {
"account_email": {
"email": {
"type": "string"
},
"email": {
"user_id": {
"type": "string"
},
"video_id": {
Expand Down
25 changes: 13 additions & 12 deletions go-server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -558,21 +558,22 @@ definitions:
type: object
models.YoutubeAnalyzerReqBody:
properties:
account_email:
description: The email of the account sending the request
type: string
email:
description: The email that will be used to send the results
description: the email to send the results
type: string
user_id:
description: The account user id
type: string
video_id:
description: the video id to analyze
type: string
type: object
models.YoutubeAnalyzerRespBody:
properties:
account_email:
type: string
email:
type: string
user_id:
type: string
video_id:
type: string
video_results:
Expand Down Expand Up @@ -1022,9 +1023,9 @@ paths:
description: An endpoint to retrieve all the youtube videos that a user has
analyzed, results are sorted by last_update field.
parameters:
- description: the account email
- description: the user id
in: query
name: account_email
name: user_id
required: true
type: string
- description: the queried page
Expand Down Expand Up @@ -1060,9 +1061,9 @@ paths:
name: videoId
required: true
type: string
- description: the account email
- description: the user id
in: query
name: account_email
name: user_id
required: true
type: string
produces:
Expand Down Expand Up @@ -1090,9 +1091,9 @@ paths:
name: videoId
required: true
type: string
- description: the account email
- description: the user_id
in: query
name: account_email
name: user_id
required: true
type: string
- description: the queried page
Expand Down
Loading