Esempio n. 1
0
// JSONTest is a testing page only, it will be removed when no longer needed.
func JSONTest(c *gin.Context) {
	// user := auth.AuthenticatedUser(c)
	user := auth.User{
		ID:        345,
		Username:  "******",
		Email:     "*****@*****.**",
		Active:    true,
		Superuser: false,
		Groups: []auth.Group{
			{
				ID:   1,
				Name: "Group 1",
				Permissions: []auth.Permission{
					{ID: 1, Name: "user-create", Description: "Can create users"},
					{ID: 2, Name: "user-update", Description: "Can update users"},
					{ID: 3, Name: "user-delete", Description: "Can delete users"},
					{ID: 4, Name: "user-read", Description: "Can read users"},
				},
			},
			{
				ID:   2,
				Name: "Group 2",
				Permissions: []auth.Permission{
					{ID: 4, Name: "user-read", Description: "Can read users"},
				},
			},
		},
	}
	json, _ := jsonapi.MarshalToJSON(user)

	c.Data(200, "application/vnd.api+json", json)
}
Esempio n. 2
0
//----------------------------------------------------------------------------
// GET /api/credentials
//----------------------------------------------------------------------------
func credentialsIndex(c *gin.Context) {
	var credentials []Credential

	db.Find(&credentials)

	json, err := jsonapi.MarshalToJSON(credentials)
	if err != nil {
		c.String(500, "Internal Server Error:"+err.Error())
		return
	}

	c.Data(200, "application/vnd.api+json", json)
}
Esempio n. 3
0
//----------------------------------------------------------------------------
// POST /api/credentials
//----------------------------------------------------------------------------
func credentialsCreate(c *gin.Context) {
	var newCredential Credential

	if err := c.BindWith(&newCredential, JsonApiBinding{}); err != nil {
		fmt.Println(err)

		// TODO: Render JSON API error to client
		return
	}

	db.Create(&newCredential)

	json, err := jsonapi.MarshalToJSON(newCredential)
	if err != nil {
		c.String(500, "Internal Server Error:"+err.Error())
		return
	}

	c.Data(201, CONTENT_TYPE, json)
}
Esempio n. 4
0
//-----------------------------------------------------------------
// POST /api/user
//-----------------------------------------------------------------
func createUser(c *gin.Context) {
	var newUser User

	if err := c.BindWith(&newUser, JsonApiBinding{}); err != nil {
		fmt.Println(err)

		// TODO: Reender JSON API error to client
		return
	}

	newUser.hashPassword()

	db.Create(&newUser)

	json, err := jsonapi.MarshalToJSON(newUser)
	if err != nil {
		c.String(500, "Internal Server Error:"+err.Error())
		return
	}

	c.Data(201, CONTENT_TYPE, json)
}
Esempio n. 5
0
//----------------------------------------------------------------------------
// PUT /api/credentials/:id
//----------------------------------------------------------------------------
func credentialsUpdate(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		return
	}

	savedCredential := &Credential{ID: id}

	query := db.First(savedCredential)
	if query.Error != nil {
		if query.Error.Error() == "record not found" {
			c.String(404, "record not found")
			return
		}
	}

	var intermCredential Credential
	if err := c.BindWith(&intermCredential, JsonApiBinding{}); err != nil {
		fmt.Println(err)
		return
	}

	savedCredential.EncryptedBlobB64 = intermCredential.EncryptedBlobB64

	query = db.Save(savedCredential)
	if query.Error != nil {
		fmt.Println(err)
		return
	}

	json, err := jsonapi.MarshalToJSON(savedCredential)
	if err != nil {
		fmt.Println(err)
		return
	}

	c.Data(200, "application/vnd.api+json", json)
}
Esempio n. 6
0
//----------------------------------------------------------------------------
// GET /api/credentials/:id
//----------------------------------------------------------------------------
func credentialsShow(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		return
	}

	credential := &Credential{ID: id}

	query := db.First(credential)
	if query.Error != nil {
		if query.Error.Error() == "record not found" {
			c.String(404, "record not found")
			return
		}
	}

	json, err := jsonapi.MarshalToJSON(credential)
	if err != nil {
		c.String(500, "Internal Server Error:"+err.Error())
		return
	}

	c.Data(200, "application/vnd.api+json", json)
}
Esempio n. 7
0
		api                *API
		rec                *httptest.ResponseRecorder
		payload, payloadID SomeData
	)

	BeforeEach(func() {
		api = NewAPI("v1")
		api.AddResource(SomeData{}, SomeResource{})
		rec = httptest.NewRecorder()
		payloadID = SomeData{ID: "12345", Data: "A Brezzn"}
		payload = SomeData{Data: "A Brezzn"}
	})

	Context("Create", func() {
		post := func(payload SomeData) {
			m, err := jsonapi.MarshalToJSON(payload)
			Expect(err).ToNot(HaveOccurred())
			req, err := http.NewRequest("POST", "/v1/someDatas", strings.NewReader(string(m)))
			Expect(err).ToNot(HaveOccurred())
			api.Handler().ServeHTTP(rec, req)
		}

		It("returns object with 201 created", func() {
			post(payload)
			Expect(rec.Code).To(Equal(http.StatusCreated))
			var actual SomeData
			err := jsonapi.UnmarshalFromJSON(rec.Body.Bytes(), &actual)
			Expect(err).ToNot(HaveOccurred())
			Expect(payloadID).To(Equal(actual))
		})