From 69688fcb8bd7e1e7ac739b8a63ddad6657ee0737 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Mon, 10 Dec 2012 18:07:44 +0100 Subject: [PATCH] add base team classes --- app/core/CoreEnv.scala | 4 ++++ app/core/Settings.scala | 2 ++ app/core/Text.scala | 12 +++++++++++ app/forum/TopicRepo.scala | 10 +-------- app/team/Member.scala | 22 ++++++++++++++++++++ app/team/Team.scala | 38 ++++++++++++++++++++++++++++++++++ app/team/TeamApi.scala | 8 ++++++++ app/team/TeamEnv.scala | 17 +++++++++++++++ app/team/TeamRepo.scala | 41 +++++++++++++++++++++++++++++++++++++ app/tournament/Player.scala | 2 -- todo | 1 + 11 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 app/core/Text.scala create mode 100644 app/team/Member.scala create mode 100644 app/team/Team.scala create mode 100644 app/team/TeamApi.scala create mode 100644 app/team/TeamEnv.scala create mode 100644 app/team/TeamRepo.scala diff --git a/app/core/CoreEnv.scala b/app/core/CoreEnv.scala index 70ab633fa3..54bba2705f 100644 --- a/app/core/CoreEnv.scala +++ b/app/core/CoreEnv.scala @@ -50,6 +50,10 @@ final class CoreEnv private (application: Application, val settings: Settings) { settings = settings, mongodb = mongodb.apply _) + lazy val team = new lila.team.TeamEnv( + settings = settings, + mongodb = mongodb.apply _) + lazy val lobby = new lila.lobby.LobbyEnv( app = app, settings = settings, diff --git a/app/core/Settings.scala b/app/core/Settings.scala index df5ac05836..c00d068ebc 100644 --- a/app/core/Settings.scala +++ b/app/core/Settings.scala @@ -123,6 +123,8 @@ final class Settings(config: Config, val IsDev: Boolean) { val WikiCollectionPage = getString("wiki.collection.page") val WikiGitUrl = getString("wiki.git_url") + val TeamCollectionTeam = getString("team.collection.team") + val BookmarkCollectionBookmark = getString("bookmark.collection.bookmark") val CoreCollectionCache = getString("core.collection.cache") diff --git a/app/core/Text.scala b/app/core/Text.scala new file mode 100644 index 0000000000..4bdd59d6e9 --- /dev/null +++ b/app/core/Text.scala @@ -0,0 +1,12 @@ +package lila.core + +object Text { + + def slugify(input: String) = { + import java.text.Normalizer + val nowhitespace = input.replace(" ", "-") + val normalized = Normalizer.normalize(nowhitespace, Normalizer.Form.NFD) + val slug = """[^\w-]""".r.replaceAllIn(normalized, "") + slug.toLowerCase + } +} diff --git a/app/forum/TopicRepo.scala b/app/forum/TopicRepo.scala index 9598d68e86..dc73b0e33f 100644 --- a/app/forum/TopicRepo.scala +++ b/app/forum/TopicRepo.scala @@ -26,7 +26,7 @@ final class TopicRepo( } def nextSlug(categ: Categ, name: String, it: Int = 1): IO[String] = { - val slug = slugify(name) + (it == 1).fold("", "-" + it) + val slug = core.Text.slugify(name) + (it == 1).fold("", "-" + it) byTree(categ.slug, slug) flatMap { _.isDefined.fold( nextSlug(categ, name, it + 1), @@ -34,14 +34,6 @@ final class TopicRepo( } } - def slugify(input: String) = { - import java.text.Normalizer - val nowhitespace = input.replace(" ", "-") - val normalized = Normalizer.normalize(nowhitespace, Normalizer.Form.NFD) - val slug = """[^\w-]""".r.replaceAllIn(normalized, "") - slug.toLowerCase - } - val all: IO[List[Topic]] = io { find(DBObject()).toList } diff --git a/app/team/Member.scala b/app/team/Member.scala new file mode 100644 index 0000000000..0811454e6f --- /dev/null +++ b/app/team/Member.scala @@ -0,0 +1,22 @@ +package lila +package team + +import org.joda.time.{ DateTime, Duration } +import org.scala_tools.time.Imports._ + +import user.User + +case class Member( + id: String, + createdAt: DateTime) { + + def is(userId: String): Boolean = id == userId + def is(user: User): Boolean = is(user.id) +} + +object Member { + + def apply(user: User): Member = apply(user.id) + + def apply(user: String): Member = new Member(id = user, createdAt = DateTime.now) +} diff --git a/app/team/Team.scala b/app/team/Team.scala new file mode 100644 index 0000000000..7212cf3fd4 --- /dev/null +++ b/app/team/Team.scala @@ -0,0 +1,38 @@ +package lila +package team + +import user.User + +import org.joda.time.DateTime +import com.novus.salat.annotations.Key +import java.text.Normalizer + +case class Team( + @Key("_id") id: String, // also the url slug + name: String, + location: Option[String], + description: String, + members: List[Member], + nbMembers: Int, + createdAt: DateTime, + createdBy: String) { + + def slug = id +} + +object Team { + + def apply( + name: String, + location: Option[String], + description: String, + createdBy: User): Team = new Team( + id = core.Text.slugify(name), + name = name, + location = location, + description = description, + members = Member(createdBy) :: Nil, + nbMembers = 1, + createdAt = DateTime.now, + createdBy = createdBy.id) +} diff --git a/app/team/TeamApi.scala b/app/team/TeamApi.scala new file mode 100644 index 0000000000..754cbd8331 --- /dev/null +++ b/app/team/TeamApi.scala @@ -0,0 +1,8 @@ +package lila +package team + +import scalaz.effects._ + +final class TeamApi(repo: TeamRepo) { + +} diff --git a/app/team/TeamEnv.scala b/app/team/TeamEnv.scala new file mode 100644 index 0000000000..c64d11c992 --- /dev/null +++ b/app/team/TeamEnv.scala @@ -0,0 +1,17 @@ +package lila +package team + +import core.Settings + +import com.mongodb.casbah.MongoCollection + +final class TeamEnv( + settings: Settings, + mongodb: String ⇒ MongoCollection) { + + import settings._ + + lazy val repo = new TeamRepo(mongodb(TeamCollectionTeam)) + + lazy val api = new TeamApi(repo = repo) +} diff --git a/app/team/TeamRepo.scala b/app/team/TeamRepo.scala new file mode 100644 index 0000000000..a5e8687859 --- /dev/null +++ b/app/team/TeamRepo.scala @@ -0,0 +1,41 @@ +package lila +package team + +import com.novus.salat._ +import com.novus.salat.dao._ +import com.mongodb.casbah.MongoCollection +import com.mongodb.casbah.query.Imports._ +import scalaz.effects._ +import org.joda.time.DateTime +import org.scala_tools.time.Imports._ + +import user.User + +final class TeamRepo(collection: MongoCollection) + extends SalatDAO[Team, String](collection) { + + def byId(id: String): IO[Option[Team]] = io { + findOneById(id) + } + + def byUser(user: User): IO[List[Team]] = io { + find(userQuery(user)).sort(sortQuery).toList + } + + def saveIO(team: Team): IO[Unit] = io { + update( + selectId(team.id), + _grater asDBObject team, + upsert = true) + } + + def removeIO(team: Team): IO[Unit] = io { + remove(selectId(team.id)) + } + + def userQuery(user: User) = DBObject("members.id" -> user.id) + + def selectId(id: String) = DBObject("_id" -> id) + + val sortQuery = DBObject("updatedAt" -> -1) +} diff --git a/app/tournament/Player.scala b/app/tournament/Player.scala index 3138fe4bc1..20d3d05495 100644 --- a/app/tournament/Player.scala +++ b/app/tournament/Player.scala @@ -1,8 +1,6 @@ package lila package tournament -import com.mongodb.casbah.query.Imports._ - import user.User case class Player( diff --git a/todo b/todo index c51864ee36..823bdac271 100644 --- a/todo +++ b/todo @@ -46,3 +46,4 @@ add fullscreen mode for spectators and load new games in a loop (based on certai make chess captcha more usable tournament 404 page log off = tournament resign +teams