GetTodo lists all users todos. Parameter: {id}.
(repo repository.TodoRepository)
| 38 | // GetTodo lists all users todos. |
| 39 | // Parameter: {id}. |
| 40 | func GetTodo(repo repository.TodoRepository) iris.Handler { |
| 41 | return func(ctx iris.Context) { |
| 42 | id := ctx.Params().Get("id") |
| 43 | userID := GetUserID(ctx) |
| 44 | |
| 45 | todo, err := repo.GetByID(id) |
| 46 | if err != nil { |
| 47 | code := iris.StatusInternalServerError |
| 48 | if errors.Is(err, repository.ErrNotFound) { |
| 49 | code = iris.StatusNotFound |
| 50 | } |
| 51 | |
| 52 | ctx.StopWithError(code, err) |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | if !IsAdmin(ctx) { // admin can access any user's todos. |
| 57 | if todo.UserID != userID { |
| 58 | ctx.StopWithStatus(iris.StatusForbidden) |
| 59 | return |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | ctx.JSON(todo) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // ListTodos lists todos of the current user. |
| 68 | func ListTodos(repo repository.TodoRepository) iris.Handler { |
no test coverage detected
searching dependent graphs…