diff --git a/QubeCinema/go.mod b/QubeCinema/go.mod new file mode 100644 index 000000000..89465f7db --- /dev/null +++ b/QubeCinema/go.mod @@ -0,0 +1,3 @@ +module QUBECINEMA + +go 1.23.2 diff --git a/QubeCinema/main.go b/QubeCinema/main.go new file mode 100644 index 000000000..702489100 --- /dev/null +++ b/QubeCinema/main.go @@ -0,0 +1,19 @@ +// main.go +package main + +import ( + "QUBECINEMA/repository" + "QUBECINEMA/utils" + "fmt" +) + +func main() { + //loading csv file + if err := repository.LoadCSV("D:/cities.csv"); err != nil { + fmt.Println("Error loading locations:", err) + return + } + + utils.InitDistributors() + utils.DisplayMenu() +} diff --git a/QubeCinema/models/distributor.go b/QubeCinema/models/distributor.go new file mode 100644 index 000000000..2aab44a1c --- /dev/null +++ b/QubeCinema/models/distributor.go @@ -0,0 +1,38 @@ +// models/distributor.go +package models + +type Distributor struct { + Name string + Parent *Distributor + Includes []string + Excludes []string + Children []*Distributor +} + +// func to add regions to include +func (d *Distributor) AddInclude(location string) { + d.Includes = append(d.Includes, location) +} + +//func to add regions to exclude +func (d *Distributor) AddExclude(location string) { + d.Excludes = append(d.Excludes, location) +} + +// checking for permission in a particular location +func (d *Distributor) HasPermission(location string) bool { + for _, excl := range d.Excludes { + if location == excl { + return false + } + } + for _, incl := range d.Includes { + if location == incl { + return true + } + } + if d.Parent != nil { + return d.Parent.HasPermission(location) + } + return false +} diff --git a/QubeCinema/repository/csv_loader.go b/QubeCinema/repository/csv_loader.go new file mode 100644 index 000000000..5bcfde696 --- /dev/null +++ b/QubeCinema/repository/csv_loader.go @@ -0,0 +1,35 @@ +// repository/csv_loader.go +package repository + +import ( + "encoding/csv" + "errors" + "os" + "strings" +) + +var ValidLocations = make(map[string]bool) + +func LoadCSV(filePath string) error { + file, err := os.Open(filePath) + if err != nil { + return errors.New("unable to open CSV file") + } + defer file.Close() + + reader := csv.NewReader(file) + records, err := reader.ReadAll() + if err != nil { + return errors.New("failed to read CSV data") + } + + for _, record := range records { + location := strings.Join(record, "-") + ValidLocations[location] = true + } + return nil +} + +func ValidateLocation(location string) bool { + return ValidLocations[location] +} diff --git a/QubeCinema/services/permissions.go b/QubeCinema/services/permissions.go new file mode 100644 index 000000000..154f2361b --- /dev/null +++ b/QubeCinema/services/permissions.go @@ -0,0 +1,34 @@ +// services/permissions.go +package services + +import ( + "QUBECINEMA/models" + "fmt" +) + +var Distributors = make(map[string]*models.Distributor) + +// to assign permision from parent to child distributor +func AssignPermissions(distributor *models.Distributor, includes []string, excludes []string) { + for _, loc := range includes { + distributor.AddInclude(loc) + } + for _, loc := range excludes { + distributor.AddExclude(loc) + } + fmt.Printf("Permissions updated for %s\n", distributor.Name) +} + +// query for distributor +func QueryDistributor(name, location string) { + dist, exists := Distributors[name] + if !exists { + fmt.Println("Distributor not found.") + return + } + if dist.HasPermission(location) { + fmt.Println("YES") + } else { + fmt.Println("NO") + } +} diff --git a/QubeCinema/utils/cli.go b/QubeCinema/utils/cli.go new file mode 100644 index 000000000..306c2764c --- /dev/null +++ b/QubeCinema/utils/cli.go @@ -0,0 +1,68 @@ +package utils + +import ( + "QUBECINEMA/models" + "QUBECINEMA/services" + "fmt" + "strings" +) + +func InitDistributors() { + // adding pre loaded distributors + distributor1 := &models.Distributor{Name: "DISTRIBUTOR1"} + distributor2 := &models.Distributor{Name: "DISTRIBUTOR2", Parent: distributor1} + + distributor1.Includes = []string{"INDIA", "UNITEDSTATES"} + distributor1.Excludes = []string{"KARNATAKA-INDIA", "CHENNAI-TAMILNADU-INDIA"} + distributor2.Includes = []string{"INDIA"} + distributor2.Excludes = []string{"TAMILNADU-INDIA"} + + services.Distributors["DISTRIBUTOR1"] = distributor1 + services.Distributors["DISTRIBUTOR2"] = distributor2 +} + +func DisplayMenu() { + var choice int + for { + //cli + fmt.Println("\n1: Query Distributor") + fmt.Println("2: Assign Permissions") + fmt.Println("3: List Distributors") + fmt.Println("4: Exit") + fmt.Print("Choose an option: ") + fmt.Scan(&choice) + + switch choice { + case 1: + var name, location string + fmt.Print("Enter distributor name: ") + fmt.Scan(&name) + fmt.Print("Enter location: ") + fmt.Scan(&location) + services.QueryDistributor(name, location) + case 2: + var name, includes, excludes string + fmt.Print("Enter distributor name: ") + fmt.Scan(&name) + fmt.Print("Enter includes (comma-separated): ") + fmt.Scan(&includes) + fmt.Print("Enter excludes (comma-separated): ") + fmt.Scan(&excludes) + + if dist, ok := services.Distributors[name]; ok { + services.AssignPermissions(dist, strings.Split(includes, ","), strings.Split(excludes, ",")) + } else { + fmt.Println("Distributor not found.") + } + case 3: + for name := range services.Distributors { + fmt.Println(name) + } + case 4: + fmt.Println("Exiting.") + return + default: + fmt.Println("Invalid choice.") + } + } +}