Skip to content

Commit

Permalink
Merge pull request #416 from Dataman-Cloud/yqguo_dataman
Browse files Browse the repository at this point in the history
update catalog api
  • Loading branch information
vitan authored Sep 14, 2016
2 parents b74aee2 + aae585b commit 8d0443b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 53 deletions.
5 changes: 3 additions & 2 deletions deploy/mysql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ CREATE TABLE `catalogs` (
`bundle` longtext,
`description` longtext,
`icon_data` longtext,
`user_id` bigint(20) unsigned DEFAULT NULL,
`account_id` bigint(20) unsigned DEFAULT NULL,
`type` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
UNIQUE KEY `accountid_name` (`account_id`, `name`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

Expand Down
52 changes: 32 additions & 20 deletions src/plugins/catalog/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package catalog
import (
"strconv"

"github.com/Dataman-Cloud/crane/src/plugins/auth"
"github.com/Dataman-Cloud/crane/src/utils/cranerror"
"github.com/Dataman-Cloud/crane/src/utils/httpresponse"

Expand All @@ -23,10 +24,13 @@ const (
CodeCatalogInvalidIcon = "400-15036"
CodeCatalogDeleteFaild = "503-15037"
CodeCatalogForbiddenOperation = "403-15038"
CodeCatalogUpdateFaild = "503-15039"
CodeCatalogCreateFaild = "503-15040"
)

const (
CATALOG_SYSTEM_DEFAULT = 0
CATALOG_USER_CUSTOM = 1
)

type CatalogApi struct {
Expand All @@ -45,30 +49,34 @@ func (catalogApi *CatalogApi) GetCatalog(ctx *gin.Context) {
catalog, err := catalogApi.Get(catalogId)
if err != nil {
log.Errorf("get catalog error: %v", err)
craneerr := cranerror.NewError(CodeCatalogGetCatalogError, err.Error())
httpresponse.Error(ctx, craneerr)
httpresponse.Error(ctx, err)
return
}

httpresponse.Ok(ctx, catalog)
}

func (catalogApi *CatalogApi) ListCatalog(ctx *gin.Context) {
//account, _ := ctx.Get("account")
catalogs, err := catalogApi.List()
if err != nil {
log.Errorf("get catalog list error: %v", err)
craneerr := cranerror.NewError(CodeCatalogInvalidUser, err.Error())
httpresponse.Error(ctx, craneerr)
httpresponse.Error(ctx, err)
return
}

httpresponse.Ok(ctx, catalogs)
}

func (catalogApi *CatalogApi) CreateCatalog(ctx *gin.Context) {
account, ok := ctx.Get("account")
if !ok {
craneerr := cranerror.NewError(CodeCatalogInvalidUser, "invalid user login")
httpresponse.Error(ctx, craneerr)
return
}

var catalog Catalog
if err := ctx.BindJSON(&catalog); err != nil {
if err := ctx.Bind(&catalog); err != nil {
log.Errorf("invalid param error: %v", err)
craneerr := cranerror.NewError(CodeCatalogInvalidParam, err.Error())
httpresponse.Error(ctx, craneerr)
Expand All @@ -81,6 +89,7 @@ func (catalogApi *CatalogApi) CreateCatalog(ctx *gin.Context) {
return
} else {
catalog.IconData = iconData
catalog.Type = CATALOG_USER_CUSTOM
}

if catalog.Bundle == "" {
Expand All @@ -89,23 +98,25 @@ func (catalogApi *CatalogApi) CreateCatalog(ctx *gin.Context) {
return
}

catalogApi.Save(&catalog)
catalog.AccountId = account.(auth.Account).ID
if err := catalogApi.Save(&catalog); err != nil {
httpresponse.Error(ctx, err)
return
}
httpresponse.Ok(ctx, catalog)
}

func (catalogApi *CatalogApi) DeleteCatalog(ctx *gin.Context) {
catalogId, err := strconv.ParseUint(ctx.Param("catalog_id"), 10, 64)
if err != nil {
log.Error("invalid catalog_id")
craneerr := cranerror.NewError(CodeCatalogInvalidCatalogId, err.Error())
httpresponse.Error(ctx, craneerr)
httpresponse.Error(ctx, err)
return
}

cl, err := catalogApi.Get(catalogId)
if err != nil {
craneerr := cranerror.NewError(CodeCatalogInvalidParam, err.Error())
httpresponse.Error(ctx, craneerr)
httpresponse.Error(ctx, err)
return
}

Expand All @@ -117,8 +128,7 @@ func (catalogApi *CatalogApi) DeleteCatalog(ctx *gin.Context) {

if err = catalogApi.Delete(catalogId); err != nil {
log.Errorf("delete catalog error: %v", err)
craneerr := cranerror.NewError(CodeCatalogDeleteFaild, err.Error())
httpresponse.Error(ctx, craneerr)
httpresponse.Error(ctx, err)
return
}

Expand All @@ -127,7 +137,7 @@ func (catalogApi *CatalogApi) DeleteCatalog(ctx *gin.Context) {

func (catalogApi *CatalogApi) UpdateCatalog(ctx *gin.Context) {
var catalog Catalog
if err := ctx.BindJSON(&catalog); err != nil {
if err := ctx.Bind(&catalog); err != nil {
log.Errorf("invalid param error: %v", err)
craneerr := cranerror.NewError(CodeCatalogInvalidParam, err.Error())
httpresponse.Error(ctx, craneerr)
Expand All @@ -150,8 +160,7 @@ func (catalogApi *CatalogApi) UpdateCatalog(ctx *gin.Context) {

cl, err := catalogApi.Get(catalogId)
if err != nil {
craneerr := cranerror.NewError(CodeCatalogInvalidParam, err.Error())
httpresponse.Error(ctx, craneerr)
httpresponse.Error(ctx, err)
return
}

Expand All @@ -172,11 +181,14 @@ func (catalogApi *CatalogApi) UpdateCatalog(ctx *gin.Context) {
}

catalog.ID = cl.ID
catalog.Name = cl.Name
catalog.Bundle = cl.Bundle
catalog.Description = cl.Description
catalog.UserId = cl.UserId
catalog.Type = cl.Type
catalog.AccountId = cl.AccountId
if err = catalogApi.Update(&catalog); err != nil {
log.Errorf("update catalog error: %v", err)
craneerr := cranerror.NewError(CodeCatalogUpdateFaild, err.Error())
httpresponse.Error(ctx, craneerr)
return
}

httpresponse.Ok(ctx, "update success")
}
4 changes: 2 additions & 2 deletions src/plugins/catalog/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ func TestCreateCatalogOK(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/test", strings.NewReader("FOOBAR"))
router.ServeHTTP(w, r)
assert.Equal(t, 400, w.Code)
assert.Equal(t, 15035, ParseErrorCode(w.Body).Code)
assert.Equal(t, 401, w.Code)
assert.Equal(t, 15033, ParseErrorCode(w.Body).Code)
}
8 changes: 2 additions & 6 deletions src/plugins/catalog/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ type Catalog struct {
Bundle string `json:"Bundle" gorm:"size:65532"`
Description string `json:"Description" gorm:"size:65532"`
IconData string `json:"IconData" gorm:"size:65532"`
UserId uint64 `json:"UserId"`
AccountId uint64 `json:"AccountId"`
Type uint8 `json:"Type"`
}

const (
ICON_SIZE = 1024 * 1024 * 1024
ICON_DEFAULT = "img/default.png"
ICON_SIZE = 1024 * 1024 * 1024
)

type Size interface {
Expand All @@ -33,9 +32,6 @@ func ImageHandle(request *http.Request) (string, error) {

icon, _, err := request.FormFile("icon")
if err != nil {
if buf, err = ioutil.ReadFile(ICON_DEFAULT); err != nil {
return "", err
}
return "", nil
} else {
if fileSize, ok := icon.(Size); !ok || fileSize.Size() > ICON_SIZE {
Expand Down
46 changes: 25 additions & 21 deletions src/plugins/catalog/store.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
package catalog

import (
"errors"

log "github.com/Sirupsen/logrus"
"github.com/Dataman-Cloud/crane/src/utils/cranerror"
)

func (catalogApi *CatalogApi) Save(catalog *Catalog) error {
var catalogs []Catalog
if err := catalogApi.DbClient.Where("name = ?", catalog.Name).Find(&catalogs).Error; err != nil {
log.Errorf("get catalog error: %v", err)
return err
}

if len(catalogs) > 0 {
log.Warnf("catlog: %s already exist", catalog.Name)
return errors.New("already exist")
}

if err := catalogApi.DbClient.Save(catalog).Error; err != nil {
log.Errorf("save catalog error: %v", err)
return err
return cranerror.NewError(CodeCatalogGetCatalogError, err.Error())
}

return nil
}

func (catalogApi *CatalogApi) List() ([]Catalog, error) {
var catalogs []Catalog
err := catalogApi.DbClient.Find(&catalogs).Error
return catalogs, err
if err := catalogApi.DbClient.Find(&catalogs).Error; err != nil {
return catalogs, cranerror.NewError(CodeCatalogListCatalogError, err.Error())
}

return catalogs, nil
}

func (catalogApi *CatalogApi) Get(catalogId uint64) (Catalog, error) {
var catalog Catalog
err := catalogApi.DbClient.Where("id = ?", catalogId).First(&catalog).Error
return catalog, err
if err := catalogApi.DbClient.Where("id = ?", catalogId).First(&catalog).Error; err != nil {
return catalog, cranerror.NewError(CodeCatalogGetCatalogError, err.Error())
}

return catalog, nil
}

func (catalogApi *CatalogApi) Delete(catalogId uint64) error {
return catalogApi.DbClient.Delete(&Catalog{ID: catalogId}).Error
if err := catalogApi.DbClient.Delete(&Catalog{ID: catalogId}).Error; err != nil {
return cranerror.NewError(CodeCatalogInvalidCatalogId, err.Error())
}

return nil
}

func (catalogApi *CatalogApi) Update(catalog *Catalog) error {
if err := catalogApi.DbClient.Save(catalog).Error; err != nil {
return cranerror.NewError(CodeCatalogInvalidParam, err.Error())
}

return nil
}
16 changes: 14 additions & 2 deletions src/plugins/catalog/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func MockCatalog() *Catalog {
Bundle: "bundle",
Description: "Description",
IconData: "IconData",
UserId: 1,
AccountId: 1,
Type: 1,
}
}
Expand All @@ -48,7 +48,7 @@ func TestCatalogSave(t *testing.T) {
panic(err)
}

assert.Error(t, catalogApi.Save(MockCatalog()))
assert.Nil(t, catalogApi.Save(MockCatalog()))
}

func TestCatalogList(t *testing.T) {
Expand Down Expand Up @@ -97,3 +97,15 @@ func TestCatalogDelete(t *testing.T) {
err = catalogApi.Delete(c.ID)
assert.Nil(t, err)
}

func TestCatalogUpdate(t *testing.T) {
fakeDb := SetupDB()
defer TearDownDB(fakeDb)
catalogApi := CatalogApi{DbClient: fakeDb.Db}
err := fakeDb.Db.Create(MockCatalog()).Error
if err != nil {
panic(err)
}

assert.Nil(t, catalogApi.Update(MockCatalog()))
}

0 comments on commit 8d0443b

Please sign in to comment.