Update repositories

This commit is contained in:
Matej Kramny 2016-06-13 19:37:40 -07:00
parent 2ab74b1768
commit d960ec53f0
5 changed files with 43 additions and 6 deletions

View File

@ -83,12 +83,16 @@ func AddRepository(c *gin.Context) {
func UpdateRepository(c *gin.Context) { func UpdateRepository(c *gin.Context) {
oldRepo := c.MustGet("repository").(models.Repository) oldRepo := c.MustGet("repository").(models.Repository)
var repository models.Repository var repository struct {
Name string `json:"name" binding:"required"`
GitUrl string `json:"git_url" binding:"required"`
SshKeyID int `json:"ssh_key_id" binding:"required"`
}
if err := c.Bind(&repository); err != nil { if err := c.Bind(&repository); err != nil {
return return
} }
if _, err := database.Mysql.Exec("update project__repository set git_url=?, ssh_key_id=? where id=?", repository.GitUrl, repository.SshKeyID, oldRepo.ID); err != nil { if _, err := database.Mysql.Exec("update project__repository set name=?, git_url=?, ssh_key_id=? where id=?", repository.Name, repository.GitUrl, repository.SshKeyID, oldRepo.ID); err != nil {
panic(err) panic(err)
} }

View File

@ -75,6 +75,7 @@ func Route(r *gin.Engine) {
api.GET("/repositories", projects.GetRepositories) api.GET("/repositories", projects.GetRepositories)
api.POST("/repositories", projects.AddRepository) api.POST("/repositories", projects.AddRepository)
api.PUT("/repositories/:repository_id", projects.RepositoryMiddleware, projects.UpdateRepository)
api.DELETE("/repositories/:repository_id", projects.RepositoryMiddleware, projects.RemoveRepository) api.DELETE("/repositories/:repository_id", projects.RepositoryMiddleware, projects.RemoveRepository)
api.GET("/inventory", projects.GetInventory) api.GET("/inventory", projects.GetInventory)

View File

@ -1,5 +1,8 @@
.modal-header .modal-header
h4.modal-title Create Repository h4.modal-title
span(ng-if="!repository.id") Create Repository
span(ng-if="repository.id") Update Repository
.modal-body .modal-body
form.form-horizontal form.form-horizontal
.form-group .form-group
@ -15,6 +18,9 @@
.col-sm-6 .col-sm-6
select.form-control(ng-model="repo.ssh_key_id" ng-options="key.id as key.name for key in keys") select.form-control(ng-model="repo.ssh_key_id" ng-options="key.id as key.name for key in keys")
option(value="") -- Select SSH Key -- option(value="") -- Select SSH Key --
.modal-footer .modal-footer
button.btn.btn-default.pull-left(ng-click="$dismiss()") Dismiss button.btn.btn-default.pull-left(ng-click="$dismiss()") Dismiss
button.btn.btn-success(ng-click="$close(repo)") Create button.btn.btn-success(ng-click="$close(repo)")
span(ng-if="!repository.id") Create
span(ng-if="repository.id") Update

View File

@ -11,4 +11,6 @@ table.table
td {{ repo.name }} td {{ repo.name }}
td {{ repo.git_url }} td {{ repo.git_url }}
td {{ repo.ssh_key_id }} td {{ repo.ssh_key_id }}
td: button.btn.btn-danger.btn-xs.pull-right(ng-click="remove(repo)") delete td: .btn-group.pull-right
button.btn.btn-default.btn-xs(ng-click="update(repo)") update
button.btn.btn-danger.btn-xs(ng-click="remove(repo)") delete

View File

@ -14,8 +14,27 @@ define(function () {
}); });
} }
$scope.update = function (repo) {
$scope.getKeys(function (keys) {
var scope = $rootScope.$new();
scope.keys = keys;
scope.repo = JSON.parse(JSON.stringify(repo));
$modal.open({
templateUrl: '/tpl/projects/repositories/add.html',
scope: scope
}).result.then(function (repo) {
$http.put(Project.getURL() + '/repositories/' + repo.id, repo).success(function () {
$scope.reload();
}).error(function (_, status) {
swal('Error', 'Repository not updated: ' + status, 'error');
})
});
});
}
$scope.add = function () { $scope.add = function () {
$http.get(Project.getURL() + '/keys?type=ssh').success(function (keys) { $scope.getKeys(function (keys) {
var scope = $rootScope.$new(); var scope = $rootScope.$new();
scope.keys = keys; scope.keys = keys;
@ -33,6 +52,11 @@ define(function () {
}); });
} }
$scope.getKeys = function (cb) {
$http.get(Project.getURL() + '/keys?type=ssh')
.success(cb);
}
$scope.reload(); $scope.reload();
}]); }]);
}); });