I would use:

```
func DB interface {
ForUser(user ID) (ListRepo, error)
ForAdmin() (ListRepo, error) // or detect which one to return in User
}

type ListRepo interface {
All() ([]*List, error)
Find(id ListID) (*List, error)
Create(t *List) error
Update(t *List) error
Delete(id ListID) error
}
```

This means when you do repo := db.ForUser(admin), it will return a specific 
implementation for administrator or user; it implements specific access 
checks and audit log, or whatever is needed.
This has a benefit that any page/thing that uses ListRepo can handle either.

+ Egon

On Tuesday, 26 September 2017 20:42:45 UTC+3, Denys Koch wrote:
>
> Hi all!
>
> I need an gopher advice. What is more idiomatic and do you see any 
> problems with my solutions?
>
> Problem: I have a repository(persistence layer) for TodoLists. TodoLists 
> usually belongs to a user. My system also have super-user/admin interface 
> which can access all TodoLists. I can imagine something like this. But I 
> think it is maybe not good, because mixing not user-scoped API and user 
> scoped API
>
> type TodoListID int
>
> type TodoList struct {}
>
> type TodoListRepository interface {
>
>   Add(t *TodoList) error
>
>   Update(t *TodoList) error
>
>   Remove(id TodoListID) error
>
>   All() ([]*TodoList, error)
>
>   Find(id TodoListID) (* TodoList, error)
>
>    // user scoped API
>
>    TodoListsForUserID(id UserID) ([]*TodoList, error)
>    AddTodo(t *Todo, u UserID) 
>    RemoveTodo(t *Todo, u UserID)                                         
>                                                                       
>
> }
>
>
>
> So I got this idea: Separate the interfaces
>
> type TodoListRepository interface {
>
>   Add(t *TodoList) error
>
>   Update(t *TodoList) error
>
>   Remove(id TodoListID) error
>
>   All() ([]*TodoList, error)
>
>   Find(id TodoListID) (* TodoList, error)
> }
> type UserTodoListsRepository interface {
>
>    All() ([]*TodoList, error)
>    AddTodo(t *Todo) 
>    RemoveTodo(t *Todo)                                                   
>                                                             
>
> }
>
> So I would define a struct wich implements UserTodoListsRepository 
> interface and one struct for not user-scoped TodoListsRepository. Do I miss 
> something? Bad or good design? 
>
> Thanks all in advance!
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to