Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Dasiu committed Aug 26, 2017
0 parents commit 717d85c
Show file tree
Hide file tree
Showing 208 changed files with 15,239 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/version.conf
logs
project/project
project/target
target
tmp
.history
dist
/.idea
/*.iml
/out
/.idea_modules
/.classpath
/.project
/RUNNING_PID
/.settings
/.vagrant
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 nemoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: target/start -Dhttp.port=${PORT} ${JAVA_OPTS}
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
play-scala-slick-compile-time-di-example-project
==================

Example project for Play framework 2.6

# Configuration
* Play 2.6,
* Scala 2.12.X,
* Slick 3.2,
* Compile time DI,
* Authentication/Authorization
* Sird router composed from every module's routers
* Example full API integration tests written with help of ScalaTest
82 changes: 82 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "rapa/mint-17.3-dev"

# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
#config.vm.network "forwarded_port", guest: 9000, host: 9001, host_ip: "127.0.0.1"
#config.vm.network "forwarded_port", guest: 4200, host: 4201, host_ip: "127.0.0.1"

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
vb.gui = false
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/vagrant", "1"]
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
end
#
# View the documentation for the provider you are using for more
# information on available options.

# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end

# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# alias ng='/vagrant/web-client/node_modules/@angular/cli/bin/ng'
# alias ngserve='/vagrant/web-client/node_modules/@angular/cli/bin/ng serve --host 0.0.0.0'
# SHELL
end
5 changes: 5 additions & 0 deletions app/Filters.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import play.filters.cors.CORSFilter
import play.http._

class Filters (corsFilter: CORSFilter)
extends DefaultHttpFilters(corsFilter)
32 changes: 32 additions & 0 deletions app/articles/ArticleComponents.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package articles

import articles.controllers.{ArticleController, PageRequest}
import articles.models.ArticleMetaModel
import articles.repositories.ArticleRepo
import articles.services.ArticleService
import com.softwaremill.macwire.wire
import commons.CommonsComponents
import commons.config.WithControllerComponents
import play.api.routing.Router
import play.api.routing.sird._

import scala.concurrent.ExecutionContext
import commons.models._

trait ArticleComponents extends WithControllerComponents with CommonsComponents with WithExecutionContext {
lazy val articleController: ArticleController = wire[ArticleController]
protected lazy val articleService: ArticleService = wire[ArticleService]
protected lazy val articleRepo: ArticleRepo = wire[ArticleRepo]

val articleRoutes: Router.Routes = {
case GET(p"/articles" ? q_o"limit=${long(limit)}" & q_o"offset=${long(offset)}") =>
val theLimit = limit.getOrElse(20L)
val theOffset = offset.getOrElse(0L)

articleController.all(PageRequest(theLimit, theOffset, List(Ordering(ArticleMetaModel.modifiedAt, Descending))))
}
}

trait WithExecutionContext {
def executionContext: ExecutionContext
}
42 changes: 42 additions & 0 deletions app/articles/controllers/ArticleController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package articles.controllers

import articles.models.Article
import articles.services.ArticleService
import commons.repositories.ActionRunner
import play.api.libs.json.{Format, JsArray, Json}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}

import scala.concurrent.ExecutionContext
import commons.models._

case class PageRequest(limit: Long, offset: Long, orderings: List[Ordering])
case class Page[Model](models: Seq[Model], count: Long)

case class ArticlePage(articles: Seq[Article], articlesCount: Long)

class ArticlePageJsonMappings

object ArticlePageJsonMappings {
import mappings.ArticleJsonMappings._

implicit val articlePageFormat: Format[ArticlePage] = Json.format[ArticlePage]
}

class ArticleController(actionRunner: ActionRunner,
articleService: ArticleService,
components: ControllerComponents,
implicit private val ec: ExecutionContext) extends AbstractController(components) {

import mappings.ArticleJsonMappings._
import ArticlePageJsonMappings._

def all(pageRequest: PageRequest): Action[AnyContent] = {
Action.async {
actionRunner.runInTransaction(articleService.all(pageRequest))
.map(page => ArticlePage(page.models, page.count))
.map(Json.toJson(_))
.map(Ok(_))
}
}

}
13 changes: 13 additions & 0 deletions app/articles/controllers/mappings/ArticleJsonMappings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package articles.controllers.mappings

import articles.models.{Article, ArticleId}
import play.api.libs.json.{Format, Json, Reads, Writes}

object ArticleJsonMappings {

implicit val articleIdReads: Reads[ArticleId] = Reads((Reads.LongReads.reads(_)).andThen(_.map(ArticleId)))
implicit val articleIdWrites: Writes[ArticleId] = Writes((Writes.LongWrites.writes(_)).compose(_.value))

implicit val articleFormat: Format[Article] = Json.format[Article]

}
37 changes: 37 additions & 0 deletions app/articles/models/Article.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package articles.models

import java.time.LocalDateTime

import commons.models.{IdMetaModel, WithDateTimes}
import commons.repositories.{BaseId, WithId}
import commons.models.{IdMetaModel, Login, Property}

case class Article(id: ArticleId,
slug: String,
title: String,
description: String,
body: String,
override val createdAt: LocalDateTime,
override val modifiedAt: LocalDateTime,
)
extends WithId[Long, ArticleId]
with WithDateTimes[Article] {

override def updateCreatedAt(dateTime: LocalDateTime): Article = copy(createdAt = dateTime)

override def updateModifiedAt(dateTime: LocalDateTime): Article = copy(modifiedAt = dateTime)

}

case class ArticleId(override val id: Long) extends AnyVal with BaseId[Long]

object ArticleMetaModel extends IdMetaModel {
val slug: Property[String] = Property("slug")
val title: Property[String] = Property("title")
val description: Property[String] = Property("description")
val body: Property[String] = Property("body")

val modifiedAt: Property[LocalDateTime] = Property("modifiedAt")

override type ModelId = ArticleId
}
3 changes: 3 additions & 0 deletions app/articles/models/NewArticle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package articles.models

case class NewArticle(slug: String, title: String, description: String, body: String)
67 changes: 67 additions & 0 deletions app/articles/repositories/ArticleRepo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package articles.repositories

import articles.controllers.{Page, PageRequest}
import articles.models.{Article, ArticleId, ArticleMetaModel}
import commons.models.{IdMetaModel, Property}
import commons.repositories._
import commons.repositories.mappings.JavaTimeDbMappings
import slick.dbio.DBIO
import slick.jdbc.MySQLProfile.api.{DBIO => _, MappedTo => _, Rep => _, TableQuery => _, _}
import slick.lifted.{ProvenShape, _}

import scala.concurrent.ExecutionContext

class ArticleRepo(override protected val dateTimeProvider: DateTimeProvider,
implicit private val ec: ExecutionContext)
extends BaseRepo[ArticleId, Article, ArticleTable]
with AuditDateTimeRepo[ArticleId, Article, ArticleTable] {

def byPageRequest(pageRequest: PageRequest): DBIO[Page[Article]] = {
require(pageRequest != null)

val count = query.size.result

val slickOrderings = pageRequest.orderings.map(toSlickOrderingSupplier).reverse

var articlesQuery = query
.drop(pageRequest.offset)
.take(pageRequest.limit)
.sortBy(slickOrderings.head)

slickOrderings.tail.foreach(getSlickOrdering => {
articlesQuery = articlesQuery.sortBy(getSlickOrdering)
})

articlesQuery.result.zip(count)
.map(articlesAndCount => Page(articlesAndCount._1, articlesAndCount._2))
}


override protected val mappingConstructor: Tag => ArticleTable = new ArticleTable(_)

override protected val modelIdMapping: BaseColumnType[ArticleId] = MappedColumnType.base[ArticleId, Long](
vo => vo.value,
id => ArticleId(id)
)

override protected val metaModel: IdMetaModel = ArticleMetaModel

override protected val metaModelToColumnsMapping: Map[Property[_], (ArticleTable) => Rep[_]] = Map(
ArticleMetaModel.id -> (table => table.id),
ArticleMetaModel.modifiedAt -> (table => table.modifiedAt),
)

}

protected class ArticleTable(tag: Tag) extends IdTable[ArticleId, Article](tag, "articles")
with AuditDateTimeTable
with JavaTimeDbMappings {

def slug: Rep[String] = column(ArticleMetaModel.slug.name)
def title: Rep[String] = column(ArticleMetaModel.title.name)
def description: Rep[String] = column(ArticleMetaModel.description.name)
def body: Rep[String] = column(ArticleMetaModel.body.name)

def * : ProvenShape[Article] = (id, slug, title, description, body, createdAt, modifiedAt) <> (Article.tupled,
Article.unapply)
}
14 changes: 14 additions & 0 deletions app/articles/services/ArticleService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package articles.services

import articles.controllers.{Page, PageRequest}
import articles.models.Article
import articles.repositories.ArticleRepo
import slick.dbio.DBIO

class ArticleService(articleRepo: ArticleRepo) {

def all(pageRequest: PageRequest): DBIO[Page[Article]] = {
articleRepo.byPageRequest(pageRequest)
}

}
Loading

0 comments on commit 717d85c

Please sign in to comment.