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
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Gradle build files
.gradle/
build/

# Ignore IntelliJ IDEA project files
/.idea/
/*.iml
/out/

# Ignore Eclipse project files
/.classpath
/.project
/.settings/

# Ignore log files and tmp files
*.log
*.tmp

# Ignore operating system files
.DS_Store
Thumbs.db

# Ignore generated files
176 changes: 147 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,159 @@
# Real Image Challenge 2016
# Distributor Permissions - Kotlin Spring WebFlux

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.
## Table of Contents

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).
1. [Run](#run)
2. [API Reference](#api-reference)
- [Create Distributor](#create-distributor)
- [List Distributors](#list-distributors)
- [Get Distributor by ID](#get-distributor-by-id)
- [Add Permission](#add-permission)
- [Check Permission](#check-permission)
- [Delete Distributor](#delete-distributor)

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.
***

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`:
## Run

```
Permissions for DISTRIBUTOR2 < DISTRIBUTOR1
INCLUDE: INDIA
EXCLUDE: TAMILNADU-INDIA
```
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.
**Requirements:** JDK 17+, Gradle 8.1

If `DISTRIBUTOR2` authorizes `DISTRIBUTOR3` to handle just the city of Hubli, Karnataka, India, for example:
```
Permissions for DISTRIBUTOR3 < DISTRIBUTOR2 < DISTRIBUTOR1
INCLUDE: HUBLI-KARNATAKA-INDIA
```
Again, `DISTRIBUTOR2` cannot authorize `DISTRIBUTOR3` with a region that they themselves do not have access to.
Start the application:

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.
./gradlew bootRun

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.
***

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

For any questions or clarifications, raise an issue on this repo and we'll answer your questions as fast as we can.
### Create Distributor

**Endpoint:**
`POST /api/distributors`

**Description:**
Creates a distributor.

**Request Body:**

{
"id": "D1",
"parentId": null
}

**cURL (Windows PowerShell):**

curl -X POST -H "Content-Type: application/json" -d "{\"id\":\"D1\",\"parentId\":null}" http://localhost:8080/api/distributors

**Response:**

{
"id": "D1",
"parentId": null,
"permissions": []
}

***

### List Distributors

**Endpoint:**
`GET /api/distributors`

**Description:**
Returns all distributors.

**cURL:**

curl http://localhost:8080/api/distributors

**Response:**

[
{
"id": "D1",
"parentId": null,
"permissions": []
}
]

***

### Get Distributor by ID

**Endpoint:**
`GET /api/distributors/{id}`

**Example:**

curl http://localhost:8080/api/distributors/D1

***

### Add Permission

**Endpoint:**
`POST /api/distributors/{id}/permissions`

**Description:**
Adds a permission to a distributor.
Sub-distributors cannot include regions outside their parent’s allowed regions.

**Request Body (Include Example):**

{
"type": "INCLUDE",
"regionCode": "INDIA"
}

**Request Body (Exclude Example):**

{
"type": "EXCLUDE",
"regionCode": "KARNATAKA-INDIA"
}

**cURL (Windows PowerShell):**

curl -X POST -H "Content-Type: application/json" -d "{\"type\":\"INCLUDE\",\"regionCode\":\"INDIA\"}" http://localhost:8080/api/distributors/D1/permissions

**Response:**

{
"id": "D1",
"parentId": null,
"permissions": [
{"type": "INCLUDE", "regionCode": "INDIA"}
]
}

***

### Check Permission

**Endpoint:**
`GET /api/distributors/{id}/can/{region}`

**Description:**
Checks if a distributor can distribute in a given region (code or name).

**Example:**

curl http://localhost:8080/api/distributors/D1/can/CHENNAI-TAMILNADU-INDIA

**Response:**

YES

***

### Delete Distributor

**Endpoint:**
`DELETE /api/distributors/{id}`

**Description:**
Removes a distributor.

**Example:**

curl -X DELETE http://localhost:8080/api/distributors/D1
33 changes: 33 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile


plugins {
id("org.springframework.boot") version "3.2.2"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.9.0"
kotlin("plugin.spring") version "1.9.0"
}


group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17


repositories {
mavenCentral()
}


dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}


tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading