-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added controllers for Cart and Category, - Modified Procuct class.
- Loading branch information
friezzerr
committed
Mar 25, 2024
1 parent
d0bce08
commit f5a1a76
Showing
7 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// app/controllers/CartController.scala | ||
|
||
package controllers | ||
|
||
import javax.inject._ | ||
import play.api.mvc._ | ||
import play.api.libs.json._ | ||
|
||
import models._ | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
@Singleton | ||
class CartController @Inject()(val controllerComponents: ControllerComponents)(implicit ec: ExecutionContext) extends BaseController { | ||
|
||
private var cart: Cart = Cart(List( | ||
CartItem(1, 2), | ||
CartItem(3, 6) | ||
)) | ||
|
||
def getCart: Action[AnyContent] = Action.async { implicit request => | ||
Future.successful(Ok(Json.toJson(cart))) | ||
} | ||
|
||
def addItemToCart: Action[JsValue] = Action.async(parse.json) { implicit request => | ||
request.body.validate[CartItem] match { | ||
case JsSuccess(cartItem, _) => | ||
cart = Cart(cart.items :+ cartItem) | ||
Future.successful(Created) | ||
case JsError(_) => Future.successful(BadRequest) | ||
} | ||
} | ||
|
||
def updateCartItemQuantity(productId: Long, quantity: Int): Action[AnyContent] = Action.async { implicit request => | ||
val updatedCartItems = cart.items.map { | ||
case CartItem(id, q) if id == productId => CartItem(id, quantity) | ||
case cartItem => cartItem | ||
} | ||
cart = Cart(updatedCartItems) | ||
Future.successful(Ok) | ||
} | ||
|
||
def removeItemFromCart(productId: Long): Action[AnyContent] = Action.async { implicit request => | ||
cart.items.find(_.productId == productId) match { | ||
case Some(_) => | ||
val updatedCartItems = cart.items.filterNot(_.productId == productId) | ||
cart = Cart(updatedCartItems) | ||
Future.successful(Ok) | ||
case None => Future.successful(NotFound) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// app/controllers/CategoryController.scala | ||
|
||
package controllers | ||
|
||
import javax.inject._ | ||
import play.api.mvc._ | ||
import play.api.libs.json._ | ||
|
||
import models.Category | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
|
||
@Singleton | ||
class CategoryController @Inject()(val controllerComponents: ControllerComponents)(implicit ec: ExecutionContext) extends BaseController { | ||
|
||
private var categories: List[Category] = List( | ||
Category(1, "Category 1", "Category 1 description"), | ||
Category(2, "Category 2", "Category 2 description") | ||
) | ||
|
||
def getAllCategories: Action[AnyContent] = Action.async { implicit request => | ||
Future.successful(Ok(Json.toJson(categories))) | ||
} | ||
|
||
def getCategoryById(id: Long): Action[AnyContent] = Action.async { implicit request => | ||
categories.find(_.id == id) match { | ||
case Some(category) => Future.successful(Ok(Json.toJson(category))) | ||
case None => Future.successful(NotFound) | ||
} | ||
} | ||
|
||
def addCategory: Action[JsValue] = Action.async(parse.json) { implicit request => | ||
request.body.validate[Category] match { | ||
case JsSuccess(category, _) => | ||
categories = categories :+ category | ||
Future.successful(Created) | ||
case JsError(_) => Future.successful(BadRequest) | ||
} | ||
} | ||
|
||
def updateCategory(id: Long): Action[JsValue] = Action.async(parse.json) { implicit request => | ||
request.body.validate[Category] match { | ||
case JsSuccess(updatedCategory, _) => | ||
categories.find(_.id == id) match { | ||
case Some(_) => | ||
categories = categories.map(c => if (c.id == id) updatedCategory else c) | ||
Future.successful(Ok) | ||
case None => Future.successful(NotFound) | ||
} | ||
case JsError(_) => Future.successful(BadRequest) | ||
} | ||
} | ||
|
||
def deleteCategory(id: Long): Action[AnyContent] = Action.async { implicit request => | ||
categories.find(_.id == id) match { | ||
case Some(_) => | ||
categories = categories.filterNot(_.id == id) | ||
Future.successful(Ok) | ||
case None => Future.successful(NotFound) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// app/models/Cart.scala | ||
|
||
package models | ||
|
||
import play.api.libs.json._ | ||
|
||
case class CartItem(productId: Long, quantity: Int) | ||
|
||
object CartItem { | ||
implicit val cartItemFormat: Format[CartItem] = Json.format[CartItem] | ||
} | ||
|
||
case class Cart(items: List[CartItem]) | ||
|
||
object Cart { | ||
implicit val cartFormat: Format[Cart] = new Format[Cart] { | ||
override def reads(json: JsValue): JsResult[Cart] = | ||
json.validate[List[CartItem]].map(Cart(_)) | ||
|
||
override def writes(cart: Cart): JsValue = | ||
Json.toJson(cart.items) | ||
} | ||
|
||
def empty: Cart = Cart(Nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// app/models/Category.scala | ||
|
||
package models | ||
|
||
import play.api.libs.json._ | ||
|
||
case class Category(id: Long, name: String, description: String) | ||
|
||
object Category { | ||
implicit val categoryFormat: OFormat[Category] = Json.format[Category] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters