Permalink
Browse files

initial load

  • Loading branch information...
notnoop committed Dec 20, 2011
0 parents commit 53961411e04bb15be03c851318ecdfab53aad425
@@ -0,0 +1,6 @@
+target
+
+*.sw?
+.sw?
+
+.DS_Store
@@ -0,0 +1,31 @@
+name := "Todo"
+
+version := "1.0"
+
+scalaVersion := "2.9.1"
+
+seq(webSettings : _*)
+
+libraryDependencies ++= Seq(
+ "org.springframework" % "spring-webmvc" % "3.1.0.RELEASE",
+ "javax.validation" % "validation-api" % "1.0.0.GA",
+ "org.hibernate" % "hibernate-validator" % "4.2.0.Final",
+ "javax.servlet" % "javax.servlet-api" % "3.0.1",
+ "org.codehaus.jackson" % "jackson-mapper-asl" % "1.9.2",
+ "javax.inject" % "javax.inject" % "1",
+ "org.springframework.data" % "spring-data-mongodb" % "1.0.0.RC1",
+ "ch.qos.logback" % "logback-classic" % "1.0.0",
+ "cglib" % "cglib-nodep" % "2.2"
+)
+
+resolvers ++= Seq(
+ "release-springsource" at "http://repo.springsource.org/release/",
+ "milestone-springframework" at "http://maven.springframework.org/milestone/"
+)
+
+libraryDependencies ++= Seq(
+ "org.mortbay.jetty" % "servlet-api" % "3.0.20100224" % "provided",
+ "org.eclipse.jetty" % "jetty-server" % "8.0.0.M3" % "container, compile",
+ "org.eclipse.jetty" % "jetty-util" % "8.0.0.M3" % "container, compile",
+ "org.eclipse.jetty" % "jetty-webapp" % "8.0.0.M3" % "container, compile"
+)
@@ -0,0 +1,3 @@
+libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % (v+"-0.2.10"))
+
+addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.7.2")
@@ -0,0 +1,24 @@
+package com.notnoop.todo
+
+import org.springframework.data.annotation.Id
+import org.springframework.data.mongodb.core.mapping.Document
+
+import scala.reflect.BeanProperty
+
+@Document
+case class Todo(
+ @BeanProperty @Id var id: String,
+ @BeanProperty var content: String,
+ @BeanProperty var order: java.lang.Integer,
+ @BeanProperty var done: java.lang.Boolean) {
+
+ def this(content: String) = this(null, content, 0, false)
+ def this() = this(null)
+
+
+ override def toString() = {
+ "Todo{" + "id=" + id + ", content=" + content + ", order=" + order + ", done=" + done + '}'
+ }
+
+}
+
@@ -0,0 +1,50 @@
+package com.notnoop.todo
+
+import javax.inject.Inject
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+import org.springframework.http.HttpStatus
+import org.springframework.stereotype.Controller
+import org.springframework.util.Assert
+import org.springframework.web.bind.annotation._
+
+@Controller
+@RequestMapping(Array("/api/todo"))
+class TodoController {
+
+ @Inject
+ private var todoRepository: TodoRepository = _
+
+ val logger = LoggerFactory.getLogger(classOf[TodoController])
+
+ @RequestMapping(method = Array(RequestMethod.GET), produces = Array("application/json")) @ResponseBody
+ def findAll(): java.util.List[Todo] = {
+ todoRepository.findAll()
+ }
+
+ @RequestMapping(value = Array("{id}"),method = Array(RequestMethod.GET) ) @ResponseBody
+ def get( @PathVariable( "id" ) id: String): Todo = {
+ this.todoRepository.findById(id)
+ }
+
+ @RequestMapping(method = Array(RequestMethod.POST), consumes =
+ Array("application/json"), produces = Array("application/json"))
+ @ResponseStatus(HttpStatus.CREATED)
+ @ResponseBody
+ def create(@RequestBody todo: Todo): Todo = {
+ Assert.notNull(todo)
+ todoRepository.create(todo)
+ }
+
+ @RequestMapping(value = Array("{id}"), method = Array(RequestMethod.PUT)) @ResponseStatus(HttpStatus.OK)
+ def update(@RequestBody todo: Todo, @PathVariable id: String) {
+ Assert.isTrue(todo.id.equals(id))
+ todoRepository.update(todo)
+ }
+
+ @RequestMapping(value = Array("{id}"), method = Array(RequestMethod.DELETE)) @ResponseStatus(HttpStatus.OK)
+ def remove(@PathVariable id: String) {
+ todoRepository.remove(id)
+ }
+}
+
@@ -0,0 +1,50 @@
+package com.notnoop.todo
+
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+import javax.inject.Inject
+import org.springframework.data.mongodb.core.MongoTemplate
+import org.springframework.data.mongodb.core.query.Criteria
+import org.springframework.data.mongodb.core.query.Query
+import org.springframework.stereotype.Repository
+
+@Repository
+class TodoRepository {
+
+ @Inject
+ var mongoTemplate: MongoTemplate = _
+
+ val logger = LoggerFactory.getLogger(classOf[TodoRepository])
+
+ def create(todo: Todo): Todo = {
+ mongoTemplate.insert(todo)
+ todo
+ }
+
+ def update(todo: Todo) = {
+ mongoTemplate.save(todo)
+ todo
+ }
+
+ def findById(id: String) = {
+ mongoTemplate.findById(id, classOf[Todo])
+ }
+
+ def findAll() = {
+ mongoTemplate.findAll(classOf[Todo])
+ }
+
+ def remove(id: String) {
+ mongoTemplate.remove(new Query(Criteria.where("id").is(id)), classOf[Todo])
+ }
+
+ def removeAll() {
+ if (mongoTemplate.collectionExists(classOf[Todo])) {
+ mongoTemplate.dropCollection(classOf[Todo])
+ mongoTemplate.createCollection(classOf[Todo])
+ }
+
+ }
+
+}
+
@@ -0,0 +1,33 @@
+package com.notnoop.todo
+
+import com.mongodb.Mongo
+import java.net.UnknownHostException
+import org.springframework.context.annotation.Bean
+import org.springframework.context.annotation.ComponentScan
+import org.springframework.context.annotation.Configuration
+import org.springframework.data.mongodb.core.MongoTemplate
+import org.springframework.web.servlet.config.annotation.EnableWebMvc
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
+
+@Configuration @EnableWebMvc @ComponentScan(basePackages = Array("com.notnoop.todo"))
+class WebConfig extends WebMvcConfigurerAdapter {
+
+ override def addResourceHandlers(registry: ResourceHandlerRegistry) {
+ registry.addResourceHandler("**/*.html").addResourceLocations("/")
+ registry.addResourceHandler("**/*.js").addResourceLocations("/")
+ registry.addResourceHandler("**/*.css").addResourceLocations("/")
+ registry.addResourceHandler("**/*.png").addResourceLocations("/")
+ }
+
+ @Bean
+ def mongo() /*throws UnknownHostException*/ = {
+ new Mongo("localhost")
+ }
+
+ @Bean
+ def mongoTemplate() /*throws UnknownHostException*/ = {
+ new MongoTemplate(mongo(), "todo")
+ }
+
+}
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
+ <display-name>Scala Spring MVC 3 Web Site</display-name>
+ <servlet>
+ <servlet-name>dispatcher</servlet-name>
+ <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ <!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext
+ instead of the default XmlWebApplicationContext -->
+ <init-param>
+ <param-name>contextClass</param-name>
+ <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
+ </init-param>
+ <!-- Again, config locations must consist of one or more comma- or space-delimited
+ and fully-qualified @Configuration classes -->
+ <init-param>
+ <param-name>contextConfigLocation</param-name>
+ <param-value>com.notnoop.todo.WebConfig</param-value>
+ </init-param>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>dispatcher</servlet-name>
+ <url-pattern>/</url-pattern>
+ </servlet-mapping>
+</web-app>
Binary file not shown.
@@ -0,0 +1,84 @@
+<!DOCTYPE html>
+<html>
+
+ <head>
+ <title>Backbone Demo: Todos</title>
+ <link href="todos.css" media="all" rel="stylesheet" type="text/css"/>
+ <script src="lib/json2.js"></script>
+ <script src="lib/jquery-1.7.1.js"></script>
+ <script src="lib/underscore-1.1.6.js"></script>
+ <script src="lib/backbone-0.5.3.js"></script>
+ <script src="todos.js"></script>
+ </head>
+
+ <body>
+
+ <!-- Todo App Interface -->
+
+ <div id="todoapp">
+
+ <div class="title">
+ <h1>Todos</h1>
+ </div>
+
+ <div class="content">
+
+ <div id="create-todo">
+ <input id="new-todo" placeholder="What needs to be done?" type="text" />
+ <span class="ui-tooltip-top" style="display:none;">Press Enter to save this task</span>
+ </div>
+
+ <div id="todos">
+ <ul id="todo-list"></ul>
+ </div>
+
+ <div id="todo-stats"></div>
+
+ </div>
+
+ </div>
+
+ <ul id="instructions">
+ <li>Double-click to edit a todo.</li>
+ </ul>
+
+ <div id="credits">
+ BackboneJS exemple application created by <a href="http://jgn.me/">J&eacute;r&ocirc;me Gravel-Niquet</a><br />
+ Spring 3.1 backend created by <a href="http://twitter.com/sdeleuze">S&eacute;bastien Deleuze</a>
+ </div>
+
+ <!-- Templates -->
+
+ <script type="text/template" id="item-template">
+ <div class="todo <%= done ? 'done' : '' %>">
+ <div class="display">
+ <input class="check" type="checkbox" <%= done ? 'checked="checked"' : '' %> />
+ <div class="todo-content"></div>
+ <span class="todo-destroy"></span>
+ </div>
+ <div class="edit">
+ <input class="todo-input" type="text" value="" />
+ </div>
+ </div>
+ </script>
+
+ <script type="text/template" id="stats-template">
+ <% if (total) { %>
+ <span class="todo-count">
+ <span class="number"><%= remaining %></span>
+ <span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left.
+ </span>
+ <% } %>
+ <% if (done) { %>
+ <span class="todo-clear">
+ <a href="#">
+ Clear <span class="number-done"><%= done %></span>
+ completed <span class="word-done"><%= done == 1 ? 'item' : 'items' %></span>
+ </a>
+ </span>
+ <% } %>
+ </script>
+
+ </body>
+
+</html>
Oops, something went wrong.

0 comments on commit 5396141

Please sign in to comment.