Skip to content

Commit

Permalink
na
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusdanciu committed Apr 15, 2015
1 parent fbc9581 commit e42be22
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 115 deletions.
8 changes: 5 additions & 3 deletions shopapi/src/main/scala/net/shop/api/Model.scala
Expand Up @@ -6,6 +6,8 @@ import scala.util.Failure
import net.shift.security.User
import net.shift.security.Permissions
import net.shift.security.Permission
import net.shift.loc.Language
import net.shift.io.FileSystem

case class UserInfo(firstName: String, lastName: String, cnp: String, phone: String)
case class CompanyInfo(name: String, cif: String, regCom: String, bank: String, bankAccount: String, phone: String)
Expand Down Expand Up @@ -124,18 +126,18 @@ case class ServiceHit(year: Int, month: Int, day: Int, service: String)
case class ServiceStat(hit: ServiceHit, count: Long)

object Formatter {
def format[T: Formatter](v: T)(implicit lang: String): String = {
def format[T: Formatter](v: T)(implicit lang: Language, fs: FileSystem): String = {
implicitly[Formatter[T]].write(v)
}
}

trait Formatter[T] {
def write(value: T)(implicit lang: String): String
def write(value: T)(implicit lang: Language, fs: FileSystem): String
}

object ShopError {
def fail(msg: String) = Failure(new ShopError(msg))
def fail(e: Throwable) = Failure(new ShopError(e))
def fail(msg: String, e: Throwable) = Failure(new ShopError(msg, e))
}

case class ShopError(msg: String, e: Throwable) extends RuntimeException(msg, e) {
Expand Down
Expand Up @@ -26,18 +26,18 @@ object MongoDBPersistence extends Persistence with MongoConversions {
def productById(id: String): Try[ProductDetail] = try {
db("products").findOne(MongoDBObject("_id" -> new ObjectId(id))) match {
case Some(obj) => Success(mongoToProduct(obj))
case _ => fail("Item " + id + " not found")
case _ => fail("no.product")
}
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def allProducts: Try[Iterator[ProductDetail]] = try {
Success(for { p <- db("products").find() } yield {
mongoToProduct(p)
})
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def categoryProducts(cat: String, spec: SortSpec = NoSort): Try[Iterator[ProductDetail]] = try {
Expand All @@ -54,7 +54,7 @@ object MongoDBPersistence extends Persistence with MongoConversions {
mongoToProduct(p)
})
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def searchProducts(text: String, spec: SortSpec = NoSort): Try[Iterator[ProductDetail]] = try {
Expand All @@ -65,39 +65,39 @@ object MongoDBPersistence extends Persistence with MongoConversions {
mongoToProduct(p)
})
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def categoryById(id: String): Try[Category] = try {
db("categories").findOne(MongoDBObject("_id" -> new ObjectId(id))) match {
case Some(obj) => Success(mongoToCategory(obj))
case _ => fail("Item " + id + " not found")
case _ => fail("no.category")
}
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def allCategories: Try[Iterator[Category]] = try {
Success(for { p <- db("categories").find().sort(MongoDBObject("position" -> 1)) } yield {
mongoToCategory(p)
})
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def deleteProducts(ids: String*): Try[Int] = try {
val num = (0 /: ids)((acc, id) => db("products").remove(MongoDBObject("_id" -> new ObjectId(id))).getN)
Success(num)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def createProducts(prod: ProductDetail*): Try[Seq[String]] = try {
val mongos = prod.map(p => productToMongo(p))
db("products").insert(mongos: _*)
Success(mongos map { p => p.get("_id").getOrElse("?").toString() })
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def updateProducts(prod: ProductDetail*): Try[Seq[String]] = try {
Expand All @@ -116,15 +116,15 @@ object MongoDBPersistence extends Persistence with MongoConversions {
builder.execute()
Success(ids)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def createCategories(cats: Category*): Try[Seq[String]] = try {
val mongos = cats.map(p => categoryToMongo(p))
db("categories").insert(mongos: _*)
Success(mongos map { p => p.getOrElse("_id", "?").toString })
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def updateCategories(c: Category*): Try[Seq[String]] = try {
Expand All @@ -143,22 +143,22 @@ object MongoDBPersistence extends Persistence with MongoConversions {
builder.execute()
Success(ids)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def deleteCategories(ids: String*): Try[Int] = try {
val num = (0 /: ids)((acc, id) => db("categories").remove(MongoDBObject("_id" -> new ObjectId(id))).getN)
Success(num)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def createUsers(user: UserDetail*): Try[Seq[String]] = try {
val mongos = user.map(userToMongo(_))
db("users").insert(mongos: _*)
Success(mongos map { p => p.getOrElse("_id", "?").toString })
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def updateUsers(user: UserDetail*): Try[Seq[String]] = try {
Expand All @@ -177,22 +177,22 @@ object MongoDBPersistence extends Persistence with MongoConversions {
builder.execute()
Success(ids)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def deleteUsers(ids: String*): Try[Int] = try {
val num = (0 /: ids)((acc, id) => db("users").remove(MongoDBObject("_id" -> new ObjectId(id))).getN)
Success(num)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def allUsers: Try[Iterator[UserDetail]] = try {
Success(for { p <- db("users").find() } yield {
mongoToUser(p)
})
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error")
}

def userByEmail(email: String): Try[Option[UserDetail]] = try {
Expand All @@ -201,7 +201,7 @@ object MongoDBPersistence extends Persistence with MongoConversions {
case _ => Success(None)
}
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def createOrder(order: OrderLog*): Try[Seq[String]] = {
Expand All @@ -218,29 +218,29 @@ object MongoDBPersistence extends Persistence with MongoConversions {

Success(mongos map { _.getOrElse("_id", "?").toString })
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}
}

def updateOrderStatus(orderId: String, status: OrderStatus): Try[Boolean] = try {
val update = db("orders").update(MongoDBObject("id" -> orderId), $set(("status" -> status.index)))
Success(update.getN == 1)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def ordersByEmail(email: String): Try[Iterator[OrderLog]] = try {
Success(
db("orders").find(MongoDBObject("email" -> email)) map mongoToOrder)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def ordersByProduct(productId: String): Try[Iterator[OrderLog]] = try {
Success(
db("orders").find("items.id" $in List(productId)) map mongoToOrder)
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def storeServiceHit(h: ServiceHit): Try[String] = try {
Expand All @@ -250,15 +250,15 @@ object MongoDBPersistence extends Persistence with MongoConversions {

Success(mongo.get("_id").getOrElse("?").toString())
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

def allServiceStats(): Try[Iterator[ServiceStat]] = try {
Success(for { p <- db("servicestats").find() } yield {
mongoToServiceStat(p)
})
} catch {
case e: Exception => fail(e)
case e: Exception => fail("internal.error", e)
}

}
Expand Down
16 changes: 8 additions & 8 deletions shopweb/localization/ro.json
@@ -1,24 +1,24 @@
[
{
"code" : "100",
"name" : "no_categories",
"name" : "internal.error",
"text" : "Eroare interna"
},
{
"code" : "100",
"name" : "no.categories",
"text" : "Categoriile nu pot fi obtinute"
},
{
"code" : "101",
"name" : "no_category",
"name" : "no.category",
"text" : "Categorie inexistenta"
},
{
"code" : "102",
"name" : "no_product",
"name" : "no.product",
"text" : "Produs inexistent"
},
{
"code" : "103",
"name" : "add_to_cart",
"text" : "Adauga in cos"
},
{
"code" : "104",
"name" : "categories",
Expand Down
20 changes: 11 additions & 9 deletions shopweb/src/main/scala/net/shop/model/Formatters.scala
Expand Up @@ -2,8 +2,10 @@ package net.shop
package model

import api._
import net.shift.loc.Language
import net.shop.api.ShopError
import net.shift.loc.Loc
import net.shift.loc.Language
import net.shift.io.FileSystem

object Formatters {

Expand All @@ -15,9 +17,9 @@ object Formatters {
override def dateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'")
}

def write(err: ShopError)(implicit lang: String): String = {
def write(err: ShopError)(implicit lang: Language, fs: FileSystem): String = {
import org.json4s.native.Serialization.writePretty
writePretty(Err(err.msg))
writePretty(Err(Loc.loc0(lang)(err.msg).text))
}

case class Err(msg: String)
Expand All @@ -32,7 +34,7 @@ object Formatters {
override def dateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'")
}

def write(orders: List[OrderLog])(implicit lang: String): String = {
def write(orders: List[OrderLog])(implicit lang: Language, fs: FileSystem): String = {
import org.json4s.native.Serialization.writePretty
writePretty(orders)
}
Expand All @@ -47,7 +49,7 @@ object Formatters {
override def dateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'")
}

def write(user: UserDetail)(implicit lang: String): String = {
def write(user: UserDetail)(implicit lang: Language, fs: FileSystem): String = {
import org.json4s.native.Serialization.writePretty
writePretty(UserSummary(user.userInfo, user.companyInfo, user.email, user.addresses))
}
Expand All @@ -63,7 +65,7 @@ object Formatters {
override def dateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'")
}

def write(err: ValidationFail)(implicit lang: String): String = {
def write(err: ValidationFail)(implicit lang: Language, fs: FileSystem): String = {
import org.json4s.native.Serialization.writePretty
writePretty(err)
}
Expand All @@ -77,7 +79,7 @@ object Formatters {
override def dateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'")
}

def write(err: FieldError)(implicit lang: String): String = {
def write(err: FieldError)(implicit lang: Language, fs: FileSystem): String = {
import org.json4s.native.Serialization.writePretty
writePretty(err)
}
Expand All @@ -91,9 +93,9 @@ object Formatters {
override def dateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'")
}

def write(c: Category)(implicit lang: String): String = {
def write(c: Category)(implicit lang: Language, fs: FileSystem): String = {
import org.json4s.native.Serialization.writePretty
writePretty(CategoryJson(c.id, c.title.getOrElse(lang, "ro"), c.position))
writePretty(CategoryJson(c.id, c.title.getOrElse(lang.name, "ro"), c.position))
}

case class CategoryJson(id: Option[String], title: String, position: Int)
Expand Down
2 changes: 1 addition & 1 deletion shopweb/src/main/scala/net/shop/utils/ShopUtils.scala
Expand Up @@ -18,7 +18,7 @@ object ShopUtils {
def imagePath(variant: String, prod: ProductDetail): String =
prod.images match {
case h :: _ => s"/data/products/${prod.stringId}/$variant/${h}"
case Nil => "/static/images/noimage.png"
case Nil => ""
}

def errorTag(text: String) = <div class="error"><div><span class="sprite sprite-exclamation"/></div><span>{ text }</span></div>
Expand Down
5 changes: 3 additions & 2 deletions shopweb/src/main/scala/net/shop/web/pages/CategoryPage.scala
Expand Up @@ -5,7 +5,6 @@ import scala.util.Failure
import scala.util.Success
import scala.xml._
import scala.xml._

import net.shift._
import net.shift._
import net.shift.engine.http._
Expand All @@ -17,6 +16,7 @@ import net.shift.template.Binds._
import net.shift.template.Snippet._
import net.shop.utils.ShopUtils._
import net.shop.web.ShopApplication
import net.shop.api.ShopError

object CategoryPage extends Cart[Request] { self =>

Expand All @@ -36,10 +36,11 @@ object CategoryPage extends Cart[Request] { self =>
case "div" attributes HasClasses("info_tag_text" :: _, a) / _ => <div>{ cat.title_?(s.state.lang.name) }</div> % a
}) match {
case Success(n) => n
case Failure(ShopError(msg, _)) => errorTag(Loc.loc0(s.state.lang)(msg).text)
case Failure(f) => errorTag(f toString)
}
}
case Failure(t) => <div class="error">{ Loc.loc0(s.state.lang)("no_categories").text }</div>
case Failure(t) => <div class="error">{ Loc.loc0(s.state.lang)("no.categories").text }</div>
}

Success(s.state.initialState, prods.toSeq)
Expand Down
3 changes: 3 additions & 0 deletions shopweb/src/main/scala/net/shop/web/pages/OrderPage.scala
Expand Up @@ -23,6 +23,8 @@ import net.shop.api.Person
import net.shop.api.Company
import net.shop.api.Address
import net.shift.io.IODefaults
import net.shift.loc.Loc
import net.shop.api.ShopError

object OrderPage extends DynamicContent[OrderState] with Selectors with IODefaults {

Expand Down Expand Up @@ -99,6 +101,7 @@ object OrderPage extends DynamicContent[OrderState] with Selectors with IODefaul
case "td" attributes HasClass("c4", a) / _ => <td><ul class="userOptions">{ prod.userOptions.flatMap { o => <li>{ o._1 + " : " + o._2 }</li> } }</ul></td> % a
}) match {
case Success(n) => acc ++ n
case Failure(ShopError(msg, _)) => acc ++ errorTag(Loc.loc0(s.state.lang)(msg).text)
case Failure(f) => acc ++ errorTag(f toString)
}
case Failure(f) => errorTag(f getMessage)
Expand Down

0 comments on commit e42be22

Please sign in to comment.