Skip to content

Commit c2de3a8

Browse files
committed
add pages service
1 parent 0860e12 commit c2de3a8

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
INSERT INTO PAGES
2+
(USER_NAME, REPOSITORY_NAME, SOURCE)
3+
SELECT
4+
A.USER_NAME
5+
, A.REPOSITORY_NAME
6+
, 'gh-pages'
7+
FROM
8+
REPOSITORY A;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<changeSet>
3+
<createTable tableName="PAGES">
4+
<column name="USER_NAME" type="varchar(100)" nullable="false"/>
5+
<column name="REPOSITORY_NAME" type="varchar(100)" nullable="false"/>
6+
<column name="SOURCE" type="varchar(100)" nullable="false"/>
7+
</createTable>
8+
9+
<addPrimaryKey constraintName="IDX_PAGES_PK" tableName="PAGES" columnNames="USER_NAME, REPOSITORY_NAME"/>
10+
</changeSet>

src/main/scala/Plugin.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gitbucket.core.controller.ControllerBase
22
import gitbucket.plugin.pages.PagesController
3+
import io.github.gitbucket.solidbase.migration.{ SqlMigration, LiquibaseMigration }
34
import io.github.gitbucket.solidbase.model.Version
45

56
class Plugin extends gitbucket.core.plugin.Plugin {
@@ -16,7 +17,12 @@ class Plugin extends gitbucket.core.plugin.Plugin {
1617
new Version("0.7"),
1718
new Version("0.8"),
1819
new Version("0.9"),
19-
new Version("1.0")
20+
new Version("1.0"),
21+
new Version(
22+
"1.1",
23+
new LiquibaseMigration("update/gitbucket-page_1.1.xml"),
24+
new SqlMigration("update/gitbucket-page_1.1.sql")
25+
)
2026
)
2127

2228
override val controllers: Seq[(String, ControllerBase)] = Seq(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package gitbucket.plugin.model
2+
3+
import gitbucket.core.model._
4+
5+
object Profile extends CoreProfile
6+
with PagesComponent
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package gitbucket.plugin.model
2+
3+
trait PagesComponent { self: gitbucket.core.model.Profile =>
4+
import profile.api._
5+
6+
implicit val psColumnType = MappedColumnType.base[PageSourceType, String](ps => ps.code, code => PageSourceType.valueOf(code))
7+
8+
lazy val Pages = TableQuery[Pages]
9+
10+
class Pages(tag: Tag) extends Table[Page](tag, "PAGES") {
11+
val userName = column[String]("USER_NAME")
12+
val repositoryName = column[String]("REPOSITORY_NAME")
13+
val source = column[PageSourceType]("SOURCE")
14+
def * = (userName, repositoryName, source) <> ((Page.apply _).tupled, Page.unapply)
15+
}
16+
}
17+
18+
abstract sealed case class PageSourceType(code: String)
19+
20+
object PageSourceType {
21+
object NONE extends PageSourceType("none")
22+
object MASTER_DOCS extends PageSourceType("master /docs")
23+
object MASTER extends PageSourceType("master")
24+
object GH_PAGES extends PageSourceType("gh-pages")
25+
26+
val values: Vector[PageSourceType] = Vector(NONE, MASTER_DOCS, MASTER, GH_PAGES)
27+
28+
private val map: Map[String, PageSourceType] = values.map(enum => enum.code -> enum).toMap
29+
30+
def apply(code: String): PageSourceType = map(code)
31+
32+
def valueOf(code: String): PageSourceType = map(code)
33+
def valueOpt(code: String): Option[PageSourceType] = map.get(code)
34+
}
35+
36+
case class Page(
37+
userName: String,
38+
repositoryName: String,
39+
source: PageSourceType
40+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package gitbucket.plugin.service
2+
3+
import gitbucket.plugin.model.{ Page, PageSourceType }
4+
import gitbucket.plugin.model.Profile._
5+
import gitbucket.plugin.model.Profile.profile.blockingApi._
6+
7+
trait PagesService {
8+
9+
def getPageOptions(userName: String, repositoryName: String)(implicit s: Session): Option[Page] =
10+
Pages.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)).firstOption
11+
12+
def registerPageOptions(userName: String, repositoryName: String, source: PageSourceType)(implicit s: Session): Unit =
13+
Pages.insert(Page(userName, repositoryName, source))
14+
15+
def updatePageOptions(userName: String, repositoryName: String, source: PageSourceType)(implicit s: Session): Unit =
16+
Pages
17+
.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind))
18+
.map(t => t.source)
19+
.update(source)
20+
21+
def renameRepository(userName: String, oldRepositoryName: String, newRepositoryName: String)(implicit s: Session): Unit =
22+
Pages
23+
.filter(t => (t.userName === userName.bind) && (t.repositoryName === oldRepositoryName.bind))
24+
.map(t => t.repositoryName)
25+
.update(newRepositoryName)
26+
27+
def renameUserName(oldUserName: String, newUserName: String, repositoryName: String)(implicit s: Session): Unit =
28+
Pages
29+
.filter(t => (t.userName === oldUserName.bind) && (t.repositoryName === repositoryName.bind))
30+
.map(t => t.userName)
31+
.update(newUserName)
32+
33+
def deletePageOptions(userName: String, repositoryName: String)(implicit s: Session): Unit = {
34+
Pages.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)).delete
35+
}
36+
}

0 commit comments

Comments
 (0)