Video:

Implementing the Comment Service

February 13, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now. Feel free to reach out to me on twitter - @Elliot_F

So now that we’ve defined our comment service interface, let’s set about actually implementing the methods that we’ve defined.

Getting a Comment By ID

So let’s start off by defining the GetComment method.

// GetComment - retrieves comments by their ID from the database
func (s *Service) GetComment(ID uint) (Comment, error) {
	var comment Comment
	if result := s.DB.First(&comment, ID); result.Error != nil {
		return Comment{}, result.Error
	}
	return comment, nil
}

So GetComment retrieves comments by their ID from the database. We’re going to pass in the ID here and then say, if the result.Error is not nil, then return an empty comment struct. Otherwise, we want to return the comment and a nil error.

So, let’s explain what we’ve done here. Now. We’ve defined the new comment variable here, which is empty as it stands. We’ve then tried to retrieve the first comment from the database with the passed in ID and if it does find something, it is then populated into the original comment variable.

Getting Comments By Slug

Cool. So let’s move on to the next function that we want to implement, which is the GetCommentsBySlug function:

// GetCommentsBySlug - retrieves all comments by slug (path - /article/name/)
func (s *Service) GetCommentsBySlug(slug string) ([]Comment, error) {
	var comments []Comment
	if result := s.DB.Find(&comments).Where("slug = ?", slug); result.Error != nil {
		return []Comment{}, result.Error
	}
	return comments, nil
}

Posting New Comments

Cool, So the next one we want to do is PostComment. This will take in comment object, and that will return the comment if it has successfully saved into the database, or an error if it has failed to insert it into the database:

// PostComment - adds a new comment to the database
func (s *Service) PostComment(comment Comment) (Comment, error) {
	if result := s.DB.Save(&comment); result.Error != nil {
		return Comment{}, result.Error
	}
	return comment, nil
}

Updating Existing Comments

The next function we want to implement is the UpdateComment endpoint which will take in a uint ID and a newComment. This will fetch the existing comment from the database by it’s original ID and then call the Updates method in order to update the existing comment with the newComment object’s fields:

// UpdateComment - updates a comment by ID with new comment info
func (s *Service) UpdateComment(ID uint, newComment Comment) (Comment, error) {
	comment, err := s.GetComment(ID)
	if err != nil {
		return Comment{}, err
	}

	if result := s.DB.Model(&comment).Updates(newComment); result.Error != nil {
		return Comment{}, result.Error
	}

	return comment, nil
}

Deleting Comments

The final function we want to implement is the DeleteComment function which will take in the ID of a comment that is to be deleted from the database:

// DeleteComment - deletes a comment from the database by ID
func (s *Service) DeleteComment(ID uint) error {
	if result := s.DB.Delete(&Comment{}, ID); result.Error != nil {
		return result.Error
	}
	return nil
}