-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement create new project by rest api
- Loading branch information
1 parent
7475d6e
commit 23dbecb
Showing
10 changed files
with
119 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...m/nestapp/projects/AllProjectsResponse.kt → .../com/nestapp/projects/rest/AllProjects.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
backend/src/main/java/com/nestapp/projects/rest/CreateProject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
6 changes: 5 additions & 1 deletion
6
...va/com/nestapp/projects/ProjectDetails.kt → ...m/nestapp/projects/rest/ProjectDetails.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters