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

[Feature Request] Modify Model DB Interface for CRUD Operations #253

Open
1 task done
Melkeydev opened this issue Jun 9, 2024 · 5 comments
Open
1 task done

[Feature Request] Modify Model DB Interface for CRUD Operations #253

Melkeydev opened this issue Jun 9, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@Melkeydev
Copy link
Owner

Tell us about your feature request

Modify the DB interface to show people how to make crud operations

Disclaimer

  • I agree
@Melkeydev Melkeydev added the enhancement New feature or request label Jun 9, 2024
@Melkeydev Melkeydev self-assigned this Jun 9, 2024
@nahaktarun
Copy link

Hi @Melkeydev I want to take this issue and want to work on it. Can you please help to provide more information about the issue.

@Ujstor
Copy link
Collaborator

Ujstor commented Aug 22, 2024

@nahaktarun @Melkeydev wouldn’t mind if you worked on this. Let me know, and I can assign it to you.

@Ujstor
Copy link
Collaborator

Ujstor commented Aug 22, 2024

The idea is to provide a basic example of how to interact with the database, how abstraction is done, and how functions that interact with the database are later referenced in other packages. There have been many questions around this topic.

This is just an example of how I envision this; it can be done in other ways.

database/models.go

package database

type Todo struct {
	Id uint64 `json:"id"`
	Todo string `json:"todo"`
}

func (s *service) CreateTodo(todo string) error {

	statment := `INSERT INTO todos(todo, done) values($1, $2);`
	_, err := s.db.Query(statment, todo)
	return err
}

func (s *service) GetTodo(id uint64) (Todo, error) {
	statement := `SELECT todo, done FROM todos WHERE id=$1;`

	var todo Todo
	todo.Id = id

	row := s.db.QueryRow(statement, id)
	err := row.Scan(&todo.Todo)
	if err != nil {
		return todo, err
	}

	return todo, nil
}

database.go

type Service interface {
       Health() map[string]string
	CreateTodo(todo string) error
        GetTodo(id uint64) (Todo, error)
}

server/server.go

type Server struct {
	port int
	db   database.Service
}

A database migration tool would also be nice for creating a test table in the database:

-- +goose Up
CREATE TABLE "todos" (
    "id" SERIAL NOT NULL,
    "todo" VARCHAR(255),
    PRIMARY KEY ("id")
);

-- +goose Down
DROP TABLE "todos";

We can also add simple API endpoints in routes.go that can be hit with curl to add , retrieve, delete etc. data from db.

Now, the tricky part is that it needs to be done for all databases specified in the --driver flag. Additionally, good documentation needs to be written.

Tests would be nice, but they're not mandatory.


Update

I was thinking, and we should maybe include this as an advanced flag. The blueprint's core features are currently in a good spot, not too bloated.
CRUD endpoints can be placed in a separate file within the server package for better readability.
I am open for suggestions and ideas.

@Samarthasbhat
Copy link

Samarthasbhat commented Sep 17, 2024

Hi @Ujstor Do we need to add CRUD operations for the TODO? Can you explain more about API endpoints in the routes.go?

@Ujstor
Copy link
Collaborator

Ujstor commented Sep 30, 2024

@Samarthasbhat I think adding CRUD operations would be a good idea. Then there are endpoints that can be tested and used as a base to build on in the future, maybe adding middleware, for example.

/api/v1/todos and /api/v1/todos/{id} for GET, POST, DELETE, PUT, and PATCH will do the job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants