Created
July 16, 2021 21:56
-
-
Save sean-brydon/e5fcee560fb444ea0e1c1ecbe8394ced to your computer and use it in GitHub Desktop.
github.copilotTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create me a api server that returns users | |
package main | |
import ( | |
"fmt" | |
"net/http" | |
"strconv" | |
"strings" | |
"time" | |
"github.com/gin-gonic/gin" | |
"github.com/kataras/iris" | |
"github.com/kataras/iris/mvc" | |
"github.com/kataras/iris/websocket" | |
) | |
// User contains some data about a user | |
type User struct { | |
ID int | |
Name string | |
} | |
// Users is a slice of users | |
type Users struct { | |
Users []User | |
} | |
// GetUsers returns a slice of users | |
func GetUsers() Users { | |
return Users{ | |
Users: []User{ | |
{ID: 1, Name: "iris"}, | |
{ID: 2, Name: "gin"}, | |
{ID: 3, Name: "gonic"}, | |
{ID: 4, Name: "kataras"}, | |
{ID: 5, Name: "the man"}, | |
}, | |
} | |
} | |
// GetUser returns a user | |
func GetUser(id int) User { | |
for _, u := range GetUsers().Users { | |
if u.ID == id { | |
return u | |
} | |
} | |
return User{} | |
} | |
// UserAPI is the main controller | |
type UserAPI struct { | |
// mvc.Controller | |
// mvc.BaseController | |
} | |
// GetUser returns a user | |
func (u UserAPI) GetUser(ctx *gin.Context) { | |
id, _ := strconv.Atoi(ctx.Param("id")) | |
user := GetUser(id) | |
ctx.JSON(http.StatusOK, gin.H{ | |
"id": id, | |
"name": user.Name, | |
}) | |
} | |
// GetUsers returns a slice of users | |
func (u UserAPI) GetUsers(ctx *gin.Context) { | |
ctx.JSON(http.StatusOK, GetUsers()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment