Skip to content

Commit

Permalink
Implement create new project by rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Feb 13, 2024
1 parent 7475d6e commit 23dbecb
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 14 deletions.
33 changes: 33 additions & 0 deletions backend/api.http
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ Content-Type: application/json
"plate_height": "1000"
}

### nest big box v2

POST localhost:8080/api/nest
Content-Type: application/json

{
"project_id": "big_box_v2",
"file_counts": {
"big_box_all+0": 1,
"big_box_all+1": 1,
"big_box_all+2": 1
},
"plate_width": "1000",
"plate_height": "1000"
}


### Get all projects
GET localhost:8080/api/projects

Expand All @@ -74,4 +91,20 @@ Content-Disposition: form-data; name="file"; filename="big_box_all.dxf"

< test-data/big_box/big_box.dxf

### Add file to project big box V2
POST nest2d.online/api/project/big_box_v2/add_file
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="short_side.dxf"

< test-data/big_box_v2/short_side.dxf


### Create the project
POST nest2d.online/api/project
Content-Type: application/json

{
"name": "Big Box V2"
}
2 changes: 1 addition & 1 deletion backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.nestapp"
version = "0.3.2"
version = "0.3.3"

application {
mainClass.set("com.nestapp.Main")
Expand Down
3 changes: 1 addition & 2 deletions backend/src/main/java/com/nestapp/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ internal object Main {
route("/api") {
projectsRest(
configuration,
File("mount/projects"),
projectsRepository,
SvgFromDxf()
)
nestRestApi(
configuration,
projectsRepository,
nestedRepository,
File("mount/projects")
)

get("/version") {
Expand Down
5 changes: 3 additions & 2 deletions backend/src/main/java/com/nestapp/nest_api/NestRestApi.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nestapp.nest_api

import com.nestapp.Configuration
import com.nestapp.files.dxf.DxfPartPlacement
import com.nestapp.files.dxf.DxfApi
import com.nestapp.projects.FileId
Expand All @@ -23,9 +24,9 @@ import java.awt.Rectangle
import java.io.File

fun Route.nestRestApi(
configuration: Configuration,
projectsRepository: ProjectsRepository,
nestedRepository: NestedRepository,
projectsFolder: File,
) {
post("/nest") {
val nestInput = call.receive<NestInput>()
Expand All @@ -35,7 +36,7 @@ fun Route.nestRestApi(
}

val id = nestedRepository.getNextId()
val result = nest(id, nestInput, projectsRepository, projectsFolder)
val result = nest(id, nestInput, projectsRepository, configuration.projectsFolder)
nestedRepository.addNested(result)

val nestedOutput = NestedOutput(id = id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.nestapp.projects

import com.nestapp.Configuration
import com.nestapp.files.SvgFromDxf
import com.nestapp.projects.rest.allProjects
import com.nestapp.projects.rest.createProject
import com.nestapp.projects.rest.projectDetails
import io.ktor.http.ContentDisposition
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
Expand All @@ -22,20 +25,20 @@ import java.io.File

fun Route.projectsRest(
configuration: Configuration,
projectsFolder: File,
projectsRepository: ProjectsRepository,
svgFromDxf: SvgFromDxf,
) {
allProjects(configuration, projectsRepository)
projectDetails(configuration, projectsRepository)
createProject(configuration, projectsRepository)

get("/preview/{project_id}/{file_id}") {
val project = project(projectsRepository)

val fileId = FileId(call.parameters["file_id"] ?: throw Exception("file_id not found"))
val svgFileName = project.files[fileId]?.svgFile ?: throw Exception("file not found")

val svgFile = File(projectsFolder, "${project.id.value}/${fileId.value}/${svgFileName}")
val svgFile = File(configuration.projectsFolder, "${project.id.value}/${fileId.value}/${svgFileName}")

call.response.header(
HttpHeaders.ContentDisposition,
Expand All @@ -47,17 +50,17 @@ fun Route.projectsRest(
call.respondFile(svgFile)
}

fileUploader(projectsRepository, projectsFolder, svgFromDxf)
fileUploader(configuration, projectsRepository, svgFromDxf)
}

private fun Route.fileUploader(
configuration: Configuration,
projectsRepository: ProjectsRepository,
projectsFolder: File,
svgFromDxf: SvgFromDxf,
) {
post("/project/{project_id}/add_file") {
val project = project(projectsRepository)
val projectFolder = File(projectsFolder, project.id.value)
val projectFolder = File(configuration.projectsFolder, project.id.value)
val multipartData = call.receiveMultipart()

multipartData.forEachPart { part ->
Expand All @@ -75,7 +78,7 @@ private fun Route.fileUploader(
File(fileFolder, fileName).writeBytes(fileBytes)

val projectFile = addFileToProject(
projectsFolder = projectsFolder,
projectsFolder = configuration.projectsFolder,
projectId = project.id,
fileId = FileId(fileId),
svgFromDxf = svgFromDxf,
Expand Down
10 changes: 10 additions & 0 deletions backend/src/main/java/com/nestapp/projects/ProjectsRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class ProjectsRepository(
return getProjectRoot().projects
}

fun add(project: Project) {
val root = getProjectRoot()
val projects = root.projects.toMutableMap()
if (projects.containsKey(project.id)) {
throw IllegalArgumentException("Project with id ${project.id} already exists")
}
projects[project.id] = project
saveProjectRoot(root.copy(projects = projects.toMap()))
}

@OptIn(ExperimentalSerializationApi::class)
@Synchronized
private fun getProjectRoot(): ProjectsRoot {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.nestapp.projects
package com.nestapp.projects.rest

import com.nestapp.Configuration
import com.nestapp.projects.ProjectId
import com.nestapp.projects.ProjectsRepository
import io.ktor.http.ContentDisposition
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
Expand Down
51 changes: 51 additions & 0 deletions backend/src/main/java/com/nestapp/projects/rest/CreateProject.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.nestapp.projects.rest

import com.nestapp.Configuration
import com.nestapp.projects.Project
import com.nestapp.projects.ProjectId
import com.nestapp.projects.ProjectsRepository
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.post
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.io.File
import java.util.Locale

fun Route.createProject(
configuration: Configuration,
projectsRepository: ProjectsRepository,
) {
post("/project") {
val request = call.receive<CreateProjectRequest>()
val project = Project(
id = createProjectId(request.name),
name = request.name,
files = emptyMap(),
)

projectsRepository.add(project)

File(configuration.projectsFolder, project.id.value).mkdirs()

call.respond(HttpStatusCode.Created, project)
}
}

fun createProjectId(inputString: String): ProjectId {
if (inputString.isBlank()) {
throw IllegalArgumentException("Project name cannot be blank")
}
val filteredString = inputString.filter { it.isLetter() || it.isWhitespace() || it.isDigit() }
val entityId = filteredString.replace(" ", "_").lowercase(Locale.getDefault())
return ProjectId(entityId)
}

@Serializable
data class CreateProjectRequest(
@SerialName("name")
val name: String,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.nestapp.projects
package com.nestapp.projects.rest

import com.nestapp.Configuration
import com.nestapp.projects.FileId
import com.nestapp.projects.Project
import com.nestapp.projects.ProjectId
import com.nestapp.projects.ProjectsRepository
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
Expand Down
4 changes: 3 additions & 1 deletion nest2dvue/src/views/ProjectList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ onMounted(async () => {
}
h2 {
margin-top: 0;
margin-bottom: 20px; /* Space below paragraphs */
color: var(--color-text);
overflow-wrap: break-word;
white-space: normal;
}
</style>

0 comments on commit 23dbecb

Please sign in to comment.