Skip to content

Commit c139bfc

Browse files
committed
Added some methods
1 parent 12df521 commit c139bfc

File tree

4 files changed

+78
-15
lines changed

4 files changed

+78
-15
lines changed

app/controllers/BlogController.scala

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,66 @@ class BlogController @Inject()(val mongoRepository: MongoRepository)(implicit ex
2020
for {
2121
blogs: List[Blog] <- mongoRepository.getBlogs()
2222
} yield
23-
Ok(Json.toJson(blogs))
23+
Ok( Json.toJson( blogs ) )
2424
}
2525

26-
def addBlog(): Action[JsValue] = Action.async(parse.json) {
26+
def addBlog(): Action[JsValue] = Action.async( parse.json ) {
2727
request => {
28-
Json.fromJson[Blog](request.body) match {
28+
Json.fromJson[Blog]( request.body ) match {
2929

30-
case JsSuccess(blog, _) => {
30+
case JsSuccess( blog, _ ) => {
3131
for {
32-
result <- mongoRepository.addBlog(blog)
32+
result <- mongoRepository.addBlog( blog )
3333
} yield {
34-
Ok(s"Blog Add result $result")
34+
Ok( s"Blog Add result $result" )
3535
}
3636
}
37-
case JsError(errors) => {
38-
Future.successful(BadRequest(s"Error adding data ${Errors.show(errors)}"))
37+
case JsError( errors ) => {
38+
Future.successful( BadRequest( s"Error adding data ${Errors.show( errors )}" ) )
3939
}
4040
}
4141

4242
}
4343
}
4444

45+
def editBlog(id: String): Action[JsValue] = Action.async( parse.json ) {
46+
request => {
47+
Json.fromJson[Blog]( request.body ) match {
48+
case JsSuccess( blog, _ ) => {
49+
for {
50+
result <- mongoRepository.updateBlog( id, blog )
51+
} yield {
52+
Ok( Json.toJson( result.get ) )
53+
}
54+
}
55+
case JsError( errors ) => {
56+
Future.successful( Ok( "" ) )
57+
}
58+
}
59+
}
60+
}
61+
62+
def deleteBlog(id: String) = Action.async {
63+
for {
64+
result <- mongoRepository.deleteBlog( id )
65+
} yield {
66+
if (result) {
67+
Ok( s"Blog with $id deleted successfully" )
68+
}
69+
else
70+
NotFound( "Blog was not found" )
71+
}
72+
}
73+
4574
def getBlog(id: String) = Action.async {
4675
_ => {
4776
for {
48-
result: Option[Blog] <- mongoRepository.getBlog(id)
49-
} yield{
77+
result: Option[Blog] <- mongoRepository.getBlog( id )
78+
} yield {
5079
if (result.isEmpty)
51-
NotFound(s"Blog with Id : $id was not found")
80+
NotFound( s"Blog with Id : $id was not found" )
5281
else
53-
Ok(Json.toJson(result.get))
82+
Ok( Json.toJson( result.get ) )
5483
}
5584
}
5685
}

app/services/MongoRepository.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import reactivemongo.api.commands.WriteResult
88
import reactivemongo.api.{Cursor, ReadPreference}
99
import reactivemongo.bson.{BSONDocument, BSONObjectID}
1010
import reactivemongo.play.json._
11+
import reactivemongo.play.json.collection.JSONBatchCommands.FindAndModifyCommand
1112
import reactivemongo.play.json.collection.JSONCollection
1213

1314
import scala.concurrent.{ExecutionContext, Future}
@@ -16,8 +17,29 @@ import scala.concurrent.{ExecutionContext, Future}
1617
* Created by Ankesh Dave on 3/21/2017.
1718
*/
1819
class MongoRepository @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit executionContext: ExecutionContext) {
20+
def deleteBlog(id: String): Future[Boolean] = {
21+
for {
22+
blogCollection <- blogsFuture
23+
query = BSONDocument("_id" -> BSONObjectID.parse(id).get)
24+
result: WriteResult <- blogCollection.remove(query)
25+
} yield {
26+
result.ok
27+
}
28+
}
29+
30+
31+
def updateBlog(id: String, blog: Blog): Future[Option[Blog]] = {
32+
for {
33+
blogCollection: JSONCollection <- blogsFuture
34+
query: BSONDocument = BSONDocument("_id" -> BSONObjectID.parse(id).get)
35+
returnValue: FindAndModifyCommand.FindAndModifyResult <- blogCollection.findAndUpdate(query,blog , true)
36+
result: Option[Blog] = returnValue.result[Blog]
37+
} yield {
38+
result
39+
}
40+
}
41+
1942
def getBlog(id: String): Future[Option[Blog]] = {
20-
import play.modules.reactivemongo._
2143
for{
2244
blogCollection: JSONCollection <- blogsFuture
2345
query = BSONDocument("_id" -> BSONObjectID.parse(id).get)

conf/routes

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

2-
GET / controllers.BlogController.getBlogs()
2+
GET / controllers.BlogController.getBlogs()
3+
# Blog
4+
35
GET /blog/:id controllers.BlogController.getBlog(id: String)
4-
POST /blog/add controllers.BlogController.addBlog
6+
PUT /blog/:id controllers.BlogController.editBlog(id : String)
7+
POST /blog controllers.BlogController.addBlog
8+
DELETE /blog/:id controllers.BlogController.deleteBlog(id : String)
59

610

711
POST /cities/add controllers.CityController.create(name: String, population: Int)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package controllers
2+
3+
/**
4+
* Created by Ankesh on 28-03-2017.
5+
*/
6+
class CityControllerTest {
7+
8+
}

0 commit comments

Comments
 (0)