Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.exe
bin/
116 changes: 91 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,107 @@
# Real Image Challenge 2016
# Real Image Challenge 2016 - Distributors Permissions API

In the cinema business, a feature film is usually provided to a regional distributor based on a contract for exhibition in a particular geographical territory.
A RESTful API to determine global distribution rights for movies by checking territorial restrictions for distributors.

Each authorization is specified by a combination of included and excluded regions. For example, a distributor might be authorzied in the following manner:
```
Permissions for DISTRIBUTOR1
INCLUDE: INDIA
INCLUDE: UNITEDSTATES
EXCLUDE: KARNATAKA-INDIA
EXCLUDE: CHENNAI-TAMILNADU-INDIA
```
This allows `DISTRIBUTOR1` to distribute in any city inside the United States and India, *except* cities in the state of Karnataka (in India) and the city of Chennai (in Tamil Nadu, India).
## Getting Started

At this point, asking your program if `DISTRIBUTOR1` has permission to distribute in `CHICAGO-ILLINOIS-UNITEDSTATES` should get `YES` as the answer, and asking if distribution can happen in `CHENNAI-TAMILNADU-INDIA` should of course be `NO`. Asking if distribution is possible in `BANGALORE-KARNATAKA-INDIA` should also be `NO`, because the whole state of Karnataka has been excluded.
### Prerequisites

Sometimes, a distributor might split the work of distribution amount smaller sub-distiributors inside their authorized geographies. For instance, `DISTRIBUTOR1` might assign the following permissions to `DISTRIBUTOR2`:
- Go 1.21 or higher
- Git

### Installation

```bash
git clone https://github.com/vipultripathi04/challenge2016.git
cd challenge2016
go mod download
```
Permissions for DISTRIBUTOR2 < DISTRIBUTOR1
INCLUDE: INDIA
EXCLUDE: TAMILNADU-INDIA

### Quick Start

```bash
make run
```
Now, `DISTRIBUTOR2` can distribute the movie anywhere in `INDIA`, except inside `TAMILNADU-INDIA` and `KARNATAKA-INDIA` - `DISTRIBUTOR2`'s permissions are always a subset of `DISTRIBUTOR1`'s permissions. It's impossible/invalid for `DISTRIBUTOR2` to have `INCLUDE: CHINA`, for example, because `DISTRIBUTOR1` isn't authorized to do that in the first place.

If `DISTRIBUTOR2` authorizes `DISTRIBUTOR3` to handle just the city of Hubli, Karnataka, India, for example:
The API will start on `http://localhost:8080`

## API Endpoint

### Check Distributor Permissions

```http
POST /distributors/permissions
```
Permissions for DISTRIBUTOR3 < DISTRIBUTOR2 < DISTRIBUTOR1
INCLUDE: HUBLI-KARNATAKA-INDIA

**Request Body Example:**

```json
[
{
"distributor": "DISTRIBUTOR1",
"include": [
{ "country": "INDIA" },
{ "country": "UNITED STATES" }
],
"exclude": [
{ "province": "KARNATAKA", "country": "INDIA" }
],
"locations": [
{ "city": "CHICAGO", "province": "ILLINOIS", "country": "UNITED STATES" },
{ "city": "CHENNAI", "province": "TAMIL NADU", "country": "INDIA" }
]
}
]
```
Again, `DISTRIBUTOR2` cannot authorize `DISTRIBUTOR3` with a region that they themselves do not have access to.

We've provided a CSV with the list of all countries, states and cities in the world that we know of - please use the data mentioned there for this program. *The codes you see there may be different from what you see here, so please always use the codes in the CSV*. This Readme is only an example.
**Request Parameters:**

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `distributor` | `string` | Yes | Distributor identifier |
| `include` | `[]Location` | Yes | Regions where distributor has rights |
| `exclude` | `[]Location` | No | Regions to exclude from included rights |
| `locations` | `[]Location` | Yes | Locations to check permissions for |

**Response (200 OK):**

Write a program in any language you want (If you're here from Gophercon, use Go :D) that does this. Feel free to make your own input and output format / command line tool / GUI / Webservice / whatever you want. Feel free to hold the dataset in whatever structure you want, but try not to use external databases - as far as possible stick to your langauage without bringing in MySQL/Postgres/MongoDB/Redis/Etc.
```json
[
{
"distributor": "DISTRIBUTOR1",
"permissions": "DISTRIBUTOR1 can distribute in CHICAGO-ILLINOIS-UNITED STATES"
},
{
"distributor": "DISTRIBUTOR1",
"permissions": "DISTRIBUTOR1 cannot distribute in CHENNAI-TAMIL NADU-INDIA"
}
]
```

To submit a solution, fork this repo and send a Pull Request on Github.
## Project Structure

For any questions or clarifications, raise an issue on this repo and we'll answer your questions as fast as we can.
```
.
├── cmd/api/
│ └── main.go # API entry point
├── internal/
│ ├── config/ # Configuration
│ ├── handler/ # HTTP handlers
│ ├── model/ # Request/response models
│ ├── repository/ # Data access layer
│ └── service/ # Business logic
├── data/
│ └── cities.csv # City database
├── go.mod # Module file
└── README.md
```

## Commands

```bash
make run # Run the API
make build # Build executable
make clean # Remove build artifacts
make test # Run tests
make help # Display help
```
39 changes: 39 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"golang/internal/config"
"golang/internal/handler"
"golang/internal/repository"
"golang/internal/service"
"log"
"net/http"

"github.com/gorilla/mux"
)

func main() {
// Load configuration
cfg := config.NewConfig()

// Initialize repository
citiesFilePath, err := repository.GetCitiesFilePath()
if err != nil {
log.Fatalf("Failed to get cities file path: %v", err)
}
citiesRepo := repository.NewCitiesRepository(citiesFilePath)

// Initialize service
distributorService := service.NewDistributorService(citiesRepo)

// Initialize handler
distributorHandler := handler.NewDistributorHandler(distributorService)

// Create router and register routes
router := mux.NewRouter()
router.HandleFunc("/distributors/permissions", distributorHandler.GetPermissions).Methods(http.MethodPost)

// Start server
listenAddr := cfg.Port
log.Printf("About to listen on port %s.", listenAddr)
log.Fatal(http.ListenAndServe(":"+listenAddr, router))
}
Loading