Skip to content
Merged

Dev #35

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
40 changes: 19 additions & 21 deletions internal/repository/forum/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (r *forumRepository) UpdateAnswer(ctx context.Context, answer *models.Answe
},
}
filter := bson.M{
"_id": *questionId,
"_id": *questionId,
"answers._id": *answerId,
}
result, err := r.collection.UpdateOne(ctx, filter, update)
Expand All @@ -123,7 +123,7 @@ func (r *forumRepository) UpdateAnswer(ctx context.Context, answer *models.Answe

func (r *forumRepository) DeleteAnswer(ctx context.Context, questionId, answerId *primitive.ObjectID) error {
filter := bson.M{
"_id": *questionId,
"_id": *questionId,
"answers._id": *answerId,
}
update := bson.M{
Expand All @@ -141,7 +141,6 @@ func (r *forumRepository) DeleteAnswer(ctx context.Context, questionId, answerId
return nil
}


// Read Question Methods
func (r *forumRepository) GetQuestionByID(ctx context.Context, questionId *primitive.ObjectID) (*models.Question, error) {
var question models.Question
Expand Down Expand Up @@ -212,7 +211,6 @@ func (r *forumRepository) GetQuestionsByCourse(ctx context.Context, courseId *pr
SetSkip(int64((page - 1) * limit)).
SetLimit(int64(limit))


// Manage Filtering
filter := bson.M{"course_id": *courseId}

Expand Down Expand Up @@ -251,7 +249,7 @@ func (r *forumRepository) GetQuestionsByCourse(ctx context.Context, courseId *pr
// Read Answer Methods
func (r *forumRepository) GetAnswerByID(ctx context.Context, questionId, answerId *primitive.ObjectID) (*models.Answer, error) {
filter := bson.M{
"_id": *questionId,
"_id": *questionId,
"answers._id": *answerId,
}
var question models.Question
Expand All @@ -271,7 +269,7 @@ func (r *forumRepository) GetAnswerByID(ctx context.Context, questionId, answerI
}

// Question voting methods
func (r* forumRepository) UpvoteQuestion(ctx context.Context, questionId *primitive.ObjectID, userId *primitive.Binary) error {
func (r *forumRepository) UpvoteQuestion(ctx context.Context, questionId *primitive.ObjectID, userId *primitive.Binary) error {
new, err := r.new_vote_question(ctx, userId, questionId, "user_upvotes", "user_downvotes", 1)
if err != nil {
return errors.NewInternalServerError("upvote question: " + err.Error())
Expand All @@ -290,7 +288,7 @@ func (r* forumRepository) UpvoteQuestion(ctx context.Context, questionId *primit
return errors.NewConflictError("question already upvoted")
}

func (r* forumRepository) DownvoteQuestion(ctx context.Context, questionId *primitive.ObjectID, userId *primitive.Binary) error {
func (r *forumRepository) DownvoteQuestion(ctx context.Context, questionId *primitive.ObjectID, userId *primitive.Binary) error {
new, err := r.new_vote_question(ctx, userId, questionId, "user_downvotes", "user_upvotes", -1)
if err != nil {
return errors.NewInternalServerError("downvote question: " + err.Error())
Expand All @@ -315,8 +313,8 @@ func (r *forumRepository) DeleteVoteQuestion(
) error {
// Intentar sacar upvote
filterUp := bson.M{
"_id": *questionId,
"user_upvotes": *userId,
"_id": *questionId,
"user_upvotes": *userId,
}
updateUp := bson.M{
"$pull": bson.M{"user_upvotes": *userId},
Expand All @@ -332,8 +330,8 @@ func (r *forumRepository) DeleteVoteQuestion(

// Intentar sacar downvote
filterDown := bson.M{
"_id": *questionId,
"user_downvotes": *userId,
"_id": *questionId,
"user_downvotes": *userId,
}
updateDown := bson.M{
"$pull": bson.M{"user_downvotes": *userId},
Expand Down Expand Up @@ -403,7 +401,7 @@ func (r *forumRepository) DownvoteAnswer(ctx context.Context, questionId, answer
return errors.NewConflictError("question already downvoted")
}

func (r *forumRepository) DeleteVoteAnswer(ctx context.Context, questionId, answerId *primitive.ObjectID, userId *primitive.Binary,) error {
func (r *forumRepository) DeleteVoteAnswer(ctx context.Context, questionId, answerId *primitive.ObjectID, userId *primitive.Binary) error {
// Intentar sacar upvote
filter := bson.M{"_id": *questionId}
updateUp := bson.M{
Expand Down Expand Up @@ -446,9 +444,8 @@ func (r *forumRepository) DeleteVoteAnswer(ctx context.Context, questionId, answ
return errors.NewBadRequestError("user has not voted on this answer")
}


// Answer Correct methods
func (r *forumRepository) SelectCorrectAnswer(ctx context.Context, questionId, answerId *primitive.ObjectID, userId *primitive.Binary,) error {
func (r *forumRepository) SelectCorrectAnswer(ctx context.Context, questionId, answerId *primitive.ObjectID, userId *primitive.Binary) error {
update := bson.M{
"$set": bson.M{
"correct_answer": *answerId,
Expand Down Expand Up @@ -479,12 +476,13 @@ func (r *forumRepository) DeleteCorrectAnswer(ctx context.Context, questionId *p
}
return nil
}

/* HELPER FUNCTIONS */
func (r* forumRepository) new_vote_question(ctx context.Context, userId *primitive.Binary, questionId *primitive.ObjectID, current string, opposite string, order int) (bool, error) {
func (r *forumRepository) new_vote_question(ctx context.Context, userId *primitive.Binary, questionId *primitive.ObjectID, current string, opposite string, order int) (bool, error) {
filter := bson.M{
"_id": *questionId,
current: bson.M{"$ne": *userId},
opposite: bson.M{"$ne": *userId},
"_id": *questionId,
current: bson.M{"$ne": *userId},
opposite: bson.M{"$ne": *userId},
}
update := bson.M{
"$addToSet": bson.M{current: *userId},
Expand All @@ -503,9 +501,9 @@ func (r* forumRepository) new_vote_question(ctx context.Context, userId *primiti

func (r *forumRepository) change_vote_question(ctx context.Context, userId *primitive.Binary, questionId *primitive.ObjectID, current string, opposite string, order int) (bool, error) {
filter := bson.M{
"_id": *questionId,
opposite: *userId,
current: bson.M{"$ne": *userId},
"_id": *questionId,
opposite: *userId,
current: bson.M{"$ne": *userId},
}
update := bson.M{
"$pull": bson.M{opposite: *userId},
Expand Down
2 changes: 2 additions & 0 deletions internal/services/questions/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (s *questionService) GetQuestionWithAnswersByID(ctx context.Context, questi
Votes: &question.Votes,
UserVote: &user_vote,
Answers: &answersDTOs,
CorrectAnswer: &question.CorrectAnswer,
}
return &question_response_dto, nil
}
Expand Down Expand Up @@ -146,6 +147,7 @@ func (s *questionService) GetQuestionsByCourse(ctx context.Context, courseId *pr
UserVote: &user_vote,
CreatedAt: &q.CreatedAt,
Answers: nil, // Answers are not included in the list of questions
CorrectAnswer: &q.CorrectAnswer,
})
}

Expand Down
Loading