Skip to content

Commit

Permalink
Create Category and Cart
Browse files Browse the repository at this point in the history
- Added controllers for Cart and Category,
- Modified Procuct class.
  • Loading branch information
friezzerr committed Mar 25, 2024
1 parent d0bce08 commit f5a1a76
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 4 deletions.
52 changes: 52 additions & 0 deletions project_2/app/controllers/CartController.scala
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)
}
}
}
63 changes: 63 additions & 0 deletions project_2/app/controllers/CategoryController.scala
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)
}
}
}
7 changes: 4 additions & 3 deletions project_2/app/controllers/ProductController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import play.api.mvc._
import play.api.libs.json._

import models.Product
import models.Category

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
Expand All @@ -15,9 +16,9 @@ import scala.concurrent.Future
class ProductController @Inject()(val controllerComponents: ControllerComponents)(implicit ec: ExecutionContext) extends BaseController {

private var products: List[Product] = List(
Product(1, "Product 1", 10.99),
Product(2, "Product 2", 20.99),
Product(3, "Product 3", 30.99)
Product(1, "Product 1", 10.99, Category(1, "Category 1", "Category 1 description")),
Product(2, "Product 2", 20.99, Category(1, "Category 1", "Category 1 description")),
Product(3, "Product 3", 30.99, Category(2, "Category 2", "Category 2 description"))
)

def getAllProducts: Action[AnyContent] = Action.async { implicit request =>
Expand Down
25 changes: 25 additions & 0 deletions project_2/app/models/Cart.scala
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)
}
11 changes: 11 additions & 0 deletions project_2/app/models/Category.scala
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]
}
3 changes: 2 additions & 1 deletion project_2/app/models/Product.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
package models

import play.api.libs.json._
import models.Category

case class Product(id: Long, name: String, price: Double)
case class Product(id: Long, name: String, price: Double, category: Category)

object Product {
implicit val productFormat: OFormat[Product] = Json.format[Product]
Expand Down
13 changes: 13 additions & 0 deletions project_2/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ GET /products/:id controllers.ProductController.getProductById
POST /products controllers.ProductController.addProduct
PUT /products/:id controllers.ProductController.updateProduct(id: Long)
DELETE /products/:id controllers.ProductController.deleteProduct(id: Long)

# CRUD endpoints for categories
GET /categories controllers.CategoryController.getAllCategories
GET /categories/:id controllers.CategoryController.getCategoryById(id: Long)
POST /categories controllers.CategoryController.addCategory
PUT /categories/:id controllers.CategoryController.updateCategory(id: Long)
DELETE /categories/:id controllers.CategoryController.deleteCategory(id: Long)

# CRUD endpoints for cart
GET /cart controllers.CartController.getCart
POST /cart controllers.CartController.addItemToCart
PUT /cart/item/:productId/quantity/:quantity controllers.CartController.updateCartItemQuantity(productId: Long, quantity: Int)
DELETE /cart/:productId controllers.CartController.removeItemFromCart(productId: Long)

0 comments on commit f5a1a76

Please sign in to comment.