code.gitea.io/gitea@v1.21.7/models/organization/team_unit.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package organization
     5  
     6  import (
     7  	"context"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	"code.gitea.io/gitea/models/perm"
    11  	"code.gitea.io/gitea/models/unit"
    12  )
    13  
    14  // TeamUnit describes all units of a repository
    15  type TeamUnit struct {
    16  	ID         int64     `xorm:"pk autoincr"`
    17  	OrgID      int64     `xorm:"INDEX"`
    18  	TeamID     int64     `xorm:"UNIQUE(s)"`
    19  	Type       unit.Type `xorm:"UNIQUE(s)"`
    20  	AccessMode perm.AccessMode
    21  }
    22  
    23  // Unit returns Unit
    24  func (t *TeamUnit) Unit() unit.Unit {
    25  	return unit.Units[t.Type]
    26  }
    27  
    28  func getUnitsByTeamID(ctx context.Context, teamID int64) (units []*TeamUnit, err error) {
    29  	return units, db.GetEngine(ctx).Where("team_id = ?", teamID).Find(&units)
    30  }
    31  
    32  // UpdateTeamUnits updates a teams's units
    33  func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) {
    34  	ctx, committer, err := db.TxContext(db.DefaultContext)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	defer committer.Close()
    39  
    40  	if _, err = db.GetEngine(ctx).Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil {
    41  		return err
    42  	}
    43  
    44  	if len(units) > 0 {
    45  		if err = db.Insert(ctx, units); err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	return committer.Commit()
    51  }