Skip to content

Commit 8cbd5bb

Browse files
committed
Respond with domain CreatedAt time.
1 parent b9d467a commit 8cbd5bb

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

apiserver/controllers/domains/domains.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func Index(c *gin.Context) {
3838
return
3939
}
4040

41+
domainsAsJSON := []interface{}{}
42+
4143
c.JSON(http.StatusOK, gin.H{
4244
"domains": domNames,
4345
})
@@ -110,6 +112,12 @@ func Create(c *gin.Context) {
110112
return
111113
}
112114

115+
// Re-fetch from db to get correct timestamps.
116+
if err := db.First(dom, dom.ID).Error; err != nil {
117+
controllers.InternalServerError(c, err)
118+
return
119+
}
120+
113121
if proj.ActiveDeploymentID != nil {
114122
j, err := job.NewWithJSON(queues.Deploy, &messages.DeployJobData{
115123
DeploymentID: *proj.ActiveDeploymentID,

apiserver/controllers/domains/domains_test.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,20 @@ var _ = Describe("Domains", func() {
314314
_, err := b.ReadFrom(res.Body)
315315
Expect(err).To(BeNil())
316316

317+
dom := &domain.Domain{}
318+
err = db.Last(dom).Error
319+
Expect(err).To(BeNil())
320+
321+
createdAtJSON, err := dom.CreatedAt.MarshalJSON()
322+
Expect(err).To(BeNil())
323+
317324
Expect(res.StatusCode).To(Equal(http.StatusCreated))
318-
Expect(b.String()).To(MatchJSON(`{
325+
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
319326
"domain": {
320-
"name": "www.foo-bar-express.com"
327+
"name": "www.foo-bar-express.com",
328+
"created_at": %s
321329
}
322-
}`))
330+
}`, createdAtJSON)))
323331
})
324332
})
325333

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

380+
dom := &domain.Domain{}
381+
err = db.Last(dom).Error
382+
Expect(err).To(BeNil())
383+
384+
createdAtJSON, err := dom.CreatedAt.MarshalJSON()
385+
Expect(err).To(BeNil())
386+
372387
Expect(res.StatusCode).To(Equal(http.StatusCreated))
373-
Expect(b.String()).To(MatchJSON(`{
388+
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
374389
"domain": {
375-
"name": "www.foo-bar-express.com"
390+
"name": "www.foo-bar-express.com",
391+
"created_at": %s
376392
}
377-
}`))
393+
}`, createdAtJSON)))
378394
})
379395

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

419+
dom := &domain.Domain{}
420+
err = db.Last(dom).Error
421+
Expect(err).To(BeNil())
422+
423+
createdAtJSON, err := dom.CreatedAt.MarshalJSON()
424+
Expect(err).To(BeNil())
425+
403426
Expect(res.StatusCode).To(Equal(http.StatusCreated))
404-
Expect(b.String()).To(MatchJSON(`{
427+
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
405428
"domain": {
406-
"name": "www.foo-bar-express.com"
429+
"name": "www.foo-bar-express.com",
430+
"created_at": %s
407431
}
408-
}`))
432+
}`, createdAtJSON)))
409433
})
410434

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

456+
createdAtJSON, err := dom.CreatedAt.MarshalJSON()
457+
Expect(err).To(BeNil())
458+
432459
Expect(res.StatusCode).To(Equal(http.StatusCreated))
433-
Expect(b.String()).To(MatchJSON(`{
460+
Expect(b.String()).To(MatchJSON(fmt.Sprintf(`{
434461
"domain": {
435-
"name": "www.foo-bar-express.com"
462+
"name": "www.foo-bar-express.com",
463+
"created_at": %s
436464
}
437-
}`))
465+
}`, createdAtJSON)))
438466

439467
Expect(dom.Name).To(Equal("www.foo-bar-express.com"))
440468
Expect(dom.ProjectID).To(Equal(proj.ID))

apiserver/models/domain/domain.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package domain
33
import (
44
"regexp"
55
"strings"
6+
"time"
67

78
"github.com/jinzhu/gorm"
89
"github.com/nitrous-io/rise-server/shared"
@@ -69,8 +70,10 @@ func (d *Domain) Validate() map[string]string {
6970
// Returns a struct that can be converted to JSON
7071
func (d *Domain) AsJSON() interface{} {
7172
return struct {
72-
Name string `json:"name"`
73+
Name string `json:"name"`
74+
CreatedAt time.Time `json:"created_at"`
7375
}{
7476
d.Name,
77+
d.CreatedAt,
7578
}
7679
}

0 commit comments

Comments
 (0)