Company Delete Endpoint done

This commit is contained in:
Lukas Bachschwell 2018-11-16 09:44:09 +01:00
parent 40ac601c2d
commit dec75bea91
3 changed files with 67 additions and 1 deletions

View File

@ -26,6 +26,7 @@ func (c *CompanyUserController) URLMapping() {
c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete)
c.Mapping("Delete", c.DeleteCompany)
}
// Post ...
@ -240,7 +241,6 @@ func (c *CompanyUserController) Delete() {
c.ServeJSONError("Error deleting Company User")
return
}
fmt.Println(userCompanyMapping)
err = models.DeleteUserCompanyMap(systemO, userCompanyMapping.ID)
if err != nil {
@ -250,3 +250,49 @@ func (c *CompanyUserController) Delete() {
c.ServeJSONSuccess("Successfully deleted!")
}
}
// DeleteCompany ...
// @Title Delete Company
// @Description Delete the entire Company
// @Success 200 {string} delete success!
// @Failure 403 failed
// @router /deletecompany [delete]
func (c *CompanyUserController) DeleteCompany() {
if currentUser.Role != constants.RoleOwner {
c.ServeJSONError("Must be Owner to delete a company")
return
}
systemDB := companydb.GetSystemDatabase()
systemO, err := orm.NewOrmWithDB("postgres", "default", systemDB)
// first check how many users are left
ucm, err := models.GetUserCompanyMapsByCompanyName(systemO, jwtSession.CompanyName)
if err != nil {
c.ServeJSONError("Error deleting User Company Relation")
return
}
for _, uc := range ucm {
systemO.Begin()
err = models.DeleteUserCompanyMap(systemO, uc.ID)
if err != nil {
c.ServeJSONError("Error deleting User Company Relation")
systemO.Rollback()
return
}
}
// Second check and delete the database
err = companydb.DeleteDatabase(jwtSession.CompanyName)
if err != nil {
systemO.Rollback()
c.ServeJSONError("Error deleting Company Database")
return
} else {
systemO.Commit()
c.ServeJSONSuccess("Successfully deleted!")
}
}

View File

@ -140,6 +140,18 @@ func GetAllUserCompanyMap(o orm.Ormer, query map[string]string, fields []string,
return nil, err
}
// GetUserCompanyMapsByCompanyName retrieves all UserCompanyMap matches a certain company. Returns empty list if
// no records exist
func GetUserCompanyMapsByCompanyName(o orm.Ormer, companyName string) (l []UserCompanyMap, err error) {
qs := o.QueryTable(new(UserCompanyMap)).Filter("company", companyName)
_, err = qs.All(&l)
if err == nil {
return l, nil
}
return nil, err
}
// UpdateUserCompanyMapById updates UserCompanyMap by Id and returns error if the record to be updated doesn't exist
func UpdateUserCompanyMapById(o orm.Ormer, m *UserCompanyMap) (err error) {
v := UserCompanyMap{ID: m.ID}

View File

@ -103,6 +103,14 @@ func init() {
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["multitenantStack/controllers:CompanyUserController"] = append(beego.GlobalControllerRouter["multitenantStack/controllers:CompanyUserController"],
beego.ControllerComments{
Method: "DeleteCompany",
Router: `/deletecompany`,
AllowHTTPMethods: []string{"delete"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["multitenantStack/controllers:ContactController"] = append(beego.GlobalControllerRouter["multitenantStack/controllers:ContactController"],
beego.ControllerComments{
Method: "Post",