Skip to content

Commit

Permalink
Respond with domain CreatedAt time.
Browse files Browse the repository at this point in the history
  • Loading branch information
chuyeow committed Aug 26, 2016
1 parent b9d467a commit 8cbd5bb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
8 changes: 8 additions & 0 deletions apiserver/controllers/domains/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func Index(c *gin.Context) {
return
}

domainsAsJSON := []interface{}{}

c.JSON(http.StatusOK, gin.H{
"domains": domNames,
})
Expand Down Expand Up @@ -110,6 +112,12 @@ func Create(c *gin.Context) {
return
}

// Re-fetch from db to get correct timestamps.
if err := db.First(dom, dom.ID).Error; err != nil {
controllers.InternalServerError(c, err)
return
}

if proj.ActiveDeploymentID != nil {
j, err := job.NewWithJSON(queues.Deploy, &messages.DeployJobData{
DeploymentID: *proj.ActiveDeploymentID,
Expand Down
52 changes: 40 additions & 12 deletions apiserver/controllers/domains/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,20 @@ var _ = Describe("Domains", func() {
_, err := b.ReadFrom(res.Body)
Expect(err).To(BeNil())

dom := &domain.Domain{}
err = db.Last(dom).Error
Expect(err).To(BeNil())

createdAtJSON, err := dom.CreatedAt.MarshalJSON()
Expect(err).To(BeNil())

Expect(res.StatusCode).To(Equal(http.StatusCreated))
Expect(b.String()).To(MatchJSON(`{
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
"domain": {
"name": "www.foo-bar-express.com"
"name": "www.foo-bar-express.com",
"created_at": %s
}
}`))
}`, createdAtJSON)))
})
})

Expand Down Expand Up @@ -369,12 +377,20 @@ var _ = Describe("Domains", func() {
_, err := b.ReadFrom(res.Body)
Expect(err).To(BeNil())

dom := &domain.Domain{}
err = db.Last(dom).Error
Expect(err).To(BeNil())

createdAtJSON, err := dom.CreatedAt.MarshalJSON()
Expect(err).To(BeNil())

Expect(res.StatusCode).To(Equal(http.StatusCreated))
Expect(b.String()).To(MatchJSON(`{
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
"domain": {
"name": "www.foo-bar-express.com"
"name": "www.foo-bar-express.com",
"created_at": %s
}
}`))
}`, createdAtJSON)))
})

It("creates a domain record in the DB", func() {
Expand All @@ -400,12 +416,20 @@ var _ = Describe("Domains", func() {
_, err := b.ReadFrom(res.Body)
Expect(err).To(BeNil())

dom := &domain.Domain{}
err = db.Last(dom).Error
Expect(err).To(BeNil())

createdAtJSON, err := dom.CreatedAt.MarshalJSON()
Expect(err).To(BeNil())

Expect(res.StatusCode).To(Equal(http.StatusCreated))
Expect(b.String()).To(MatchJSON(`{
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
"domain": {
"name": "www.foo-bar-express.com"
"name": "www.foo-bar-express.com",
"created_at": %s
}
}`))
}`, createdAtJSON)))
})

It("creates a domain record in the DB", func() {
Expand All @@ -429,12 +453,16 @@ var _ = Describe("Domains", func() {
_, err := b.ReadFrom(res.Body)
Expect(err).To(BeNil())

createdAtJSON, err := dom.CreatedAt.MarshalJSON()
Expect(err).To(BeNil())

Expect(res.StatusCode).To(Equal(http.StatusCreated))
Expect(b.String()).To(MatchJSON(`{
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
"domain": {
"name": "www.foo-bar-express.com"
"name": "www.foo-bar-express.com",
"created_at": %s
}
}`))
}`, createdAtJSON)))

Expect(dom.Name).To(Equal("www.foo-bar-express.com"))
Expect(dom.ProjectID).To(Equal(proj.ID))
Expand Down
5 changes: 4 additions & 1 deletion apiserver/models/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"regexp"
"strings"
"time"

"github.com/jinzhu/gorm"
"github.com/nitrous-io/rise-server/shared"
Expand Down Expand Up @@ -69,8 +70,10 @@ func (d *Domain) Validate() map[string]string {
// Returns a struct that can be converted to JSON
func (d *Domain) AsJSON() interface{} {
return struct {
Name string `json:"name"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
}{
d.Name,
d.CreatedAt,
}
}

0 comments on commit 8cbd5bb

Please sign in to comment.