randomcoding/PartsDbWebApp — Issue #13: Database update methods should check for duplicate data as well as duplicate Ids
Author: randomcoder
Created: 2011-12-01 08:01:40+00:00
State: open
Labels: bug, db

Currently the `add(item)` method only checks to see if the item is already present based on its id. This means that it is possible to add an duplicate of the item that is only different by id.

The `add(item)` method should be modified in two ways:
1. Check the db for content matches
2. Change its return type to be either an option of the added object or an option with the added object's id. In both cases, use None to indicate that the add did not happen.

In order to check for matches we should generate a query based on the object type and check to se if findOne is populated.

E.g.
{{{#scala
def objectMatchQuery(item:Identifiable): MongoDBObject = {
item match {
case addr: Address => MongoDBObject("shortName" -> addr.shortName, "country" -> addr.country)
case cust: Customer => MongoDBObject(...)
case _ => MongoDBObject.empty
}
}

def isInDb(item: Identifiable): Boolean = collection.findOne(objectMatchQuery(item)).isDefined

}}}