Skip to content
Merged

Dev #29

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
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func LoadConfig() *Config {

MONGODB_URL: getEnvOrDefault("MONGODB_URL", ""),
MONGODB_NAME: getEnvOrDefault("MONGODB_NAME", ""),

NOTIFICATIONS_SERVICE: "http://" + getEnvOrDefault("NOTIFICATIONS_SERVICE", "") + ":8000",
}
}

Expand Down
7 changes: 0 additions & 7 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,6 @@ const docTemplate = `{
"name": "questionId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Answer ID",
"name": "answerId",
"in": "path",
"required": true
}
],
"responses": {
Expand Down
7 changes: 0 additions & 7 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,6 @@
"name": "questionId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Answer ID",
"name": "answerId",
"in": "path",
"required": true
}
],
"responses": {
Expand Down
5 changes: 0 additions & 5 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,6 @@ paths:
name: questionId
required: true
type: string
- description: Answer ID
in: path
name: answerId
required: true
type: string
responses:
"200":
description: Answer without answers
Expand Down
1 change: 1 addition & 0 deletions helm-chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ service:

env:
MONGODB_NAME: "classconnect-forum"
NOTIFICATIONS_SERVICE: "notifications-microservice"
11 changes: 11 additions & 0 deletions internal/dto/push_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dto

import "github.com/google/uuid"

type PushRequest struct {
Ids uuid.UUIDs `json:"to"`
Title string `json:"title"`
Body string `json:"body"`
Type string `json:"type"`
ID string `json:"id"`
}
2 changes: 1 addition & 1 deletion internal/handlers/answers/answer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestMain(m *testing.M) {
MAX_FILE_UPLOAD_SIZE: 10 * 1024 * 1024,
}

service = answer_service.NewAnswerService(forumRepo, conf)
service = answer_service.NewAnswerService(forumRepo, nil, conf)
answerHandler = answer_handler.NewAnswerHandler(service, conf)

// router usando UUID válido del primer usuario
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/questions/question_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestMain(m *testing.M) {
MAX_FILE_UPLOAD_SIZE: 10 * 1024 * 1024,
}

service = question_service.NewQuestionService(forumRepo, conf)
service = question_service.NewQuestionService(forumRepo, nil, conf)
questionHandler = question_handler.NewQuestionHandler(service, conf)

// router usando UUID válido del primer usuario
Expand Down
5 changes: 3 additions & 2 deletions internal/router/answer_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
answer_handler "forum-microservice/internal/handlers/answers"
forum_repository "forum-microservice/internal/repository/forum"
answer_service "forum-microservice/internal/services/answers"

notification "forum-microservice/internal/utils/notifications"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
)

func InitAnswerRoutes(r *gin.Engine, db *mongo.Database, conf *config.Config) {
forumRepository := forum_repository.NewForumRepository(db.Collection("forum"), conf)
questionService := answer_service.NewAnswerService(forumRepository, conf)
pushNotifier := notification.NewPushNotifier(&conf.NOTIFICATIONS_SERVICE, conf)
questionService := answer_service.NewAnswerService(forumRepository, pushNotifier, conf)
answerHandler := answer_handler.NewAnswerHandler(questionService, conf)

group := r.Group("/question/:questionId/")
Expand Down
4 changes: 3 additions & 1 deletion internal/router/question_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
question_handler "forum-microservice/internal/handlers/questions"
forum_repository "forum-microservice/internal/repository/forum"
question_service "forum-microservice/internal/services/questions"
notifications "forum-microservice/internal/utils/notifications"

"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
)

func InitQuestionRoutes(r *gin.Engine, db *mongo.Database, conf *config.Config) {
forumRepository := forum_repository.NewForumRepository(db.Collection("forum"), conf)
questionService := question_service.NewQuestionService(forumRepository, conf)
pushNotifier := notifications.NewPushNotifier(&conf.NOTIFICATIONS_SERVICE, conf)
questionService := question_service.NewQuestionService(forumRepository, pushNotifier, conf)
questionHandler := question_handler.NewQuestionHandler(questionService, conf)

group := r.Group("/")
Expand Down
47 changes: 45 additions & 2 deletions internal/services/answers/answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (
"forum-microservice/internal/errors"
"forum-microservice/internal/models"
"forum-microservice/internal/repository/forum"
notification "forum-microservice/internal/utils/notifications"

"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type answerService struct {
repo forum.ForumRepository
noti notification.Notifier
conf *config.Config
}

func NewAnswerService(repo forum.ForumRepository, conf *config.Config) AnswerService {
func NewAnswerService(repo forum.ForumRepository, noti notification.Notifier, conf *config.Config) AnswerService {
return &answerService{
repo: repo,
noti: noti,
conf: conf,
}
}
Expand All @@ -28,6 +32,23 @@ func (s *answerService) PostAnswer(ctx context.Context, answer *models.Answer, q
return err
}

// Get original question
question, err := s.repo.GetQuestionByID(ctx, questionId)
if err != nil {
return err
}

// Continue if answer is on itself
if question.AuthorID.Subtype == answer.AuthorID.Subtype && bytes.Equal(question.AuthorID.Data, answer.AuthorID.Data) {
return nil
}
users := uuid.UUIDs{uuid.UUID(question.AuthorID.Data)}


if s.noti != nil {
s.noti.SendNewAnswer(&users, questionId)
}

return nil
}

Expand Down Expand Up @@ -69,6 +90,22 @@ func (s *answerService) UpvoteAnswer(ctx context.Context, questionId, answerId *
return err
}

// Get upvoted question
answer, err := s.repo.GetAnswerByID(ctx, questionId, answerId)
if err != nil {
return err
}

// Continue if upvote is on itself
if answer.AuthorID.Subtype == user_id.Subtype && bytes.Equal(answer.AuthorID.Data, user_id.Data) {
return nil
}
users := uuid.UUIDs{uuid.UUID(answer.AuthorID.Data)}

if s.noti != nil{
s.noti.SendUpvote(&users, questionId)
}

return nil
}

Expand Down Expand Up @@ -98,7 +135,7 @@ func (s *answerService) SelectCorrectAnswer(ctx context.Context, questionId, ans
return errors.NewUnauthorizedError("unauthorized: user cant edit this answer")
}

_, err = s.repo.GetAnswerByID(ctx, questionId, answerId)
answer, err := s.repo.GetAnswerByID(ctx, questionId, answerId)
if err != nil {
return err
}
Expand All @@ -107,6 +144,12 @@ func (s *answerService) SelectCorrectAnswer(ctx context.Context, questionId, ans
return err
}

users := uuid.UUIDs{uuid.UUID(answer.AuthorID.Data)}

if s.noti != nil {
s.noti.SendCorrectAnswer(&users, questionId)
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/services/answers/answer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestMain(m *testing.M) {
}
}

answerService = answer.NewAnswerService(forumRepo, nil)
answerService = answer.NewAnswerService(forumRepo, nil, nil)

m.Run()
}
Expand All @@ -79,7 +79,7 @@ func TestAnswer_PostAnswer(t *testing.T) {
uid := uuid.New()
binaryUUID := utils.UuidToBinary(uid)
a := models.Answer{
Content: "test-title-4",
Content: "test-title-4",
AuthorID: binaryUUID,
}
err := answerService.PostAnswer(t.Context(), &a, &q_ids[0])
Expand Down
24 changes: 21 additions & 3 deletions internal/services/questions/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import (
"forum-microservice/internal/models"
"forum-microservice/internal/repository/forum"
"forum-microservice/internal/utils"
notification "forum-microservice/internal/utils/notifications"

"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type questionService struct {
repo forum.ForumRepository
noti notification.Notifier
conf *config.Config
}

func NewQuestionService(repo forum.ForumRepository, conf *config.Config) QuestionService {
func NewQuestionService(repo forum.ForumRepository, noti notification.Notifier, conf *config.Config) QuestionService {
return &questionService{
repo: repo,
noti: noti,
conf: conf,
}
}
Expand All @@ -30,8 +34,6 @@ func (s *questionService) PostQuestion(ctx context.Context, question *models.Que
return err
}



return nil
}

Expand Down Expand Up @@ -157,6 +159,22 @@ func (s *questionService) UpvoteQuestion(ctx context.Context, questionId *primit
return err
}

// Get upvoted question
question, err := s.repo.GetQuestionByID(ctx, questionId)
if err != nil {
return err
}

// Continue if upvote is on itself
if question.AuthorID.Subtype == user_id.Subtype && bytes.Equal(question.AuthorID.Data, user_id.Data) {
return nil
}
users := uuid.UUIDs{uuid.UUID(question.AuthorID.Data)}

if s.noti != nil {
s.noti.SendUpvote(&users, questionId)
}

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/services/questions/question_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestMain(m *testing.M) {
ids = append(ids, q.ID)
}

questionService = question.NewQuestionService(forumRepo, nil)
questionService = question.NewQuestionService(forumRepo, nil, nil)

m.Run()
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestQuestion_GetQuestionWithAnswersByID_SetDefaults(t *testing.T) {
}
_ = repo.CreateQuestion(context.Background(), &q)

service := question.NewQuestionService(repo, nil)
service := question.NewQuestionService(repo, nil, nil)

// filtros sin inicializar (Page y Limit nil)
var filters dto.FiltersDTO
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestQuestion_GetQuestionsByCourse_SetDefaults(t *testing.T) {
}
_ = repo.CreateQuestion(context.Background(), &q)

service := question.NewQuestionService(repo, nil)
service := question.NewQuestionService(repo, nil, nil)

// filtros sin inicializar (Page y Limit nil)
var filters dto.FiltersDTO
Expand Down
14 changes: 14 additions & 0 deletions internal/utils/notifications/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package notification

import (
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type Notifier interface {
SendUpvote(users *uuid.UUIDs, questionId *primitive.ObjectID)

SendCorrectAnswer(users *uuid.UUIDs, questionId *primitive.ObjectID)

SendNewAnswer(users *uuid.UUIDs, questionId *primitive.ObjectID)
}
Loading