pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

SearchedLocation.scala

(1783B)


      1 package inc.pyc
      2 package model
      3 
      4 import field._
      5 import lib._
      6 import net.liftweb._
      7 import common.Box
      8 import mongodb.record._
      9 import mongodb.record.field._
     10 import record.field._
     11 import com.foursquare.rogue.LatLong
     12 
     13 class SearchedLocation private () extends MongoRecord[SearchedLocation] with ObjectIdPk[SearchedLocation] {
     14   def meta = SearchedLocation
     15   
     16   /** The total amount of times this was searched. */
     17   object total extends IntField(this)
     18   
     19   /** The string that was searched. */
     20   object name extends OptionalStringField(this, 255)
     21   
     22   /** The Geolocation of `name` the place. */
     23   object loc extends MongoCaseClassField[SearchedLocation, LatLong](this) {
     24     import Geocode._
     25     
     26     def apply(geo: GeoPoint): SearchedLocation = {
     27       val latLong = LatLong(geo.lat, geo.lng)
     28       apply(latLong)
     29     }
     30   }
     31   
     32   /** List of emails that want to be notified when a new atm is near this search location. */
     33   object emails extends MongoListField[SearchedLocation, String](this) with MongoListFieldExtra[SearchedLocation, String]
     34   
     35   override def save(safe: Boolean = true) = {
     36     val qry = SearchedLocation.find(this)
     37     
     38     if(qry.get().isDefined) {
     39       qry.modify(_.total inc 1).updateOne()
     40       this
     41     } else {
     42       total(total.get + 1)
     43       super.save()
     44     }
     45   }
     46 }
     47 
     48 object SearchedLocation extends SearchedLocation with RogueMetaRecord[SearchedLocation] {
     49   import mongodb.BsonDSL._
     50   
     51   override def collectionName = "atm.search"
     52     
     53   ensureIndex(loc.name -> "2d", unique = true)
     54   
     55   def findByName(in: String): Box[SearchedLocation] = find(name.name, in)
     56   def find(search: SearchedLocation) = this.where(_.loc eqs search.loc.get)
     57     
     58   object usa extends CountryField(SearchedLocation.createRecord) {
     59     override def defaultValue = Countries.USA
     60   }
     61 }