mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-10-31 18:46:28 +01:00
Add AccountService and ProjectService.
This commit is contained in:
@@ -16,7 +16,7 @@ abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFo
|
||||
implicit def context: Context = Context(servletContext.getContextPath, LoginUser)
|
||||
|
||||
// TODO get from session
|
||||
private val LoginUser = System.getProperty("user.name")
|
||||
private val LoginUser = "admin" //System.getProperty("user.name")
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
package app
|
||||
|
||||
import util.Directory._
|
||||
import org.scalatra._
|
||||
import service._
|
||||
import java.io.File
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.eclipse.jgit.lib._
|
||||
import org.apache.commons.io._
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
|
||||
class CreateRepositoryController extends CreateRepositoryControllerBase with ProjectService with AccountService
|
||||
|
||||
/**
|
||||
* Creates new repository.
|
||||
*/
|
||||
class CreateRepositoryController extends ControllerBase {
|
||||
trait CreateRepositoryControllerBase extends ControllerBase { self: ProjectService =>
|
||||
|
||||
case class RepositoryCreationForm(name: String, description: String)
|
||||
case class RepositoryCreationForm(name: String, description: String) // TODO Option
|
||||
|
||||
val form = mapping(
|
||||
"name" -> trim(label("Repository name", text(required, maxlength(40), repository))),
|
||||
@@ -61,6 +63,9 @@ class CreateRepositoryController extends ControllerBase {
|
||||
FileUtils.deleteDirectory(tmpdir)
|
||||
}
|
||||
|
||||
// insert to the database
|
||||
createProject(form.name, context.loginUser, Some(form.description))
|
||||
|
||||
// redirect to the repository
|
||||
redirect("/%s/%s".format(context.loginUser, form.name))
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ object Projects extends Table[Project]("PROJECT") {
|
||||
def defaultBranch = column[String]("DEFAULT_BRANCH")
|
||||
def registeredDate = column[java.sql.Date]("REGISTERED_DATE") // TODO convert java.util.Date later
|
||||
def updatedDate = column[java.sql.Date]("UPDATED_DATE")
|
||||
def lastActivityDate = column[java.sql.Date]("LAST_LOGIN_DATE")
|
||||
def lastActivityDate = column[java.sql.Date]("LAST_ACTIVITY_DATE")
|
||||
def * = projectId.? ~ projectName ~ userId ~ projectType ~ description.? ~ defaultBranch ~ registeredDate ~ updatedDate ~ lastActivityDate <> (Project, Project.unapply _)
|
||||
def ins = projectName ~ userId ~ projectType ~ description.? ~ defaultBranch ~ registeredDate ~ updatedDate ~ lastActivityDate <> ({ t => Project(None, t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8)}, { (o: Project) => Some((o.projectName, o.userId, o.projectType, o.description, o.defaultBranch, o.registeredDate, o.updatedDate, o.lastActivityDate))})
|
||||
}
|
||||
|
||||
12
src/main/scala/service/AccountService.scala
Normal file
12
src/main/scala/service/AccountService.scala
Normal file
@@ -0,0 +1,12 @@
|
||||
package service
|
||||
|
||||
import model._
|
||||
import scala.slick.driver.H2Driver.simple._
|
||||
import Database.threadLocalSession
|
||||
|
||||
trait AccountService {
|
||||
|
||||
def getAccountByUserName(userName: String): Option[Account] =
|
||||
Query(Accounts) filter(_.userName is userName.bind) firstOption
|
||||
|
||||
}
|
||||
37
src/main/scala/service/ProjectService.scala
Normal file
37
src/main/scala/service/ProjectService.scala
Normal file
@@ -0,0 +1,37 @@
|
||||
package service
|
||||
|
||||
import model._
|
||||
import scala.slick.driver.H2Driver.simple._
|
||||
import Database.threadLocalSession
|
||||
|
||||
trait ProjectService { self: AccountService =>
|
||||
|
||||
/**
|
||||
* Creates a new project.
|
||||
*
|
||||
* The project is created as public repository at first. Users can modify the project type at the repository settings
|
||||
* page after the project creation to configure the project as the private repository.
|
||||
*
|
||||
* @param projectName the project name
|
||||
* @param userName the user name of the project owner
|
||||
* @param description the project description
|
||||
* @return the created project id
|
||||
*/
|
||||
def createProject(projectName: String, userName: String, description: Option[String]): Long = {
|
||||
// TODO create a git repository also here?
|
||||
|
||||
val currentDate = new java.sql.Date(System.currentTimeMillis)
|
||||
Projects.ins returning Projects.projectId insert
|
||||
Project(
|
||||
projectId = None,
|
||||
projectName = projectName,
|
||||
userId = getAccountByUserName(userName).get.userId.get,
|
||||
projectType = 0 /* 0:public, 1:private */,
|
||||
description = description,
|
||||
defaultBranch = "master",
|
||||
registeredDate = currentDate,
|
||||
updatedDate = currentDate,
|
||||
lastActivityDate = currentDate)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user