Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QubeCinema Assignment Solution. #159

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions QubeCinema/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module QUBECINEMA

go 1.23.2
19 changes: 19 additions & 0 deletions QubeCinema/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
38 changes: 38 additions & 0 deletions QubeCinema/models/distributor.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions QubeCinema/repository/csv_loader.go
Original file line number Diff line number Diff line change
@@ -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]
}
34 changes: 34 additions & 0 deletions QubeCinema/services/permissions.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
68 changes: 68 additions & 0 deletions QubeCinema/utils/cli.go
Original file line number Diff line number Diff line change
@@ -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.")
}
}
}