Permalink
Browse files

mergeObjectInfo and refactor SearchResult

  • Loading branch information...
1 parent 08dae06 commit 7c324368653fbb2894bc88e0286a3286a7a3286a @torii-y torii-y committed Nov 2, 2016
View
@@ -283,7 +283,7 @@ function search(conn, tenantId, query, all, people) {
// { Nothing, Just } -- objects that has no person
arr.push({
personId : null,
- objectIdAndFloorId : [e, e.floorId]
+ objectAndFloorId : [e, e.floorId]
});
}
});
@@ -295,21 +295,43 @@ function search(conn, tenantId, query, all, people) {
// { Just, Just } -- people who exist in map
arr.push({
personId : personId,
- objectIdAndFloorId : [e, e.floorId]
+ objectAndFloorId : [e, e.floorId]
});
})
// { Just, Nothing } -- missing people
if(!objects.length) {
arr.push({
personId : personId,
- objectIdAndFloorId : null
+ objectAndFloorId : null
});
}
});
return Promise.resolve(arr);
});
}
+// function searchObjectsByName(conn, tenantId, name, all) {
+// var filterByPublic =
+// all ? '' : 'AND f.public = 1';
+//
+// // TODO avoid injection
+// var sql =
+// `SELECT o.*
+// FROM map2.objects AS o, map2.floors AS f
+// WHERE o.name like "%${name}%" AND o.floorId = f.id AND o.floorVersion = f.version AND f.tenantId = "${tenantId}" ${filterByPublic}
+// ORDER BY o.id, o.floorId, o.floorVersion`;
+//
+// return rdb.exec(conn, sql).then((records) => {
+// var objects = {};
+// records.forEach(record => {
+// objects[records.id] = record;// keep newest
+// });
+// return Promise.resolve(Object.keys(objects).map(objectId => {
+// return objects[objectId];
+// }));
+// });
+// }
+
function getPrototypes(conn, tenantId) {
return rdb.exec(conn, sql.select('prototypes', sql.where('tenantId', tenantId)));
}
View
@@ -119,6 +119,12 @@ app.get('/master', (req, res) => {
res.send(masterHtml);
});
+app.get('/api/1/people/search/:name', inTransaction((conn, req, res) => {
+ var token = getAuthToken(req);
+ var name = req.params.name;
+ return profileService.search(config.profileServiceRoot, token, name);
+}));
+
app.get('/api/1/people/:id', inTransaction((conn, req, res) => {
var token = getAuthToken(req);
var id = req.params.id;
@@ -303,12 +309,6 @@ app.get('/api/1/search/:query', inTransaction((conn, req, res) => {
});
}));
-app.get('/api/1/candidates/:name', inTransaction((conn, req, res) => {
- var token = getAuthToken(req);
- var name = req.params.name;
- return profileService.search(config.profileServiceRoot, token, name);
-}));
-
// TODO move to service logic
function isValidFloor(floor) {
if(!floor.name.trim()) {
View
@@ -207,7 +207,7 @@ personCandidate config name =
else
getWithoutCache
decodePeople
- (config.apiRoot ++ "/1/candidates/" ++ Http.uriEncode name)
+ (config.apiRoot ++ "/1/people/search/" ++ Http.uriEncode name)
(authorization config.token)
@@ -14,7 +14,7 @@ import Model.User as User exposing (User)
import Model.Person exposing (Person)
import Model.Object as Object exposing (..)
import Model.Prototype exposing (Prototype)
-import Model.SearchResult exposing (SearchResult)
+import Model.SearchResult as SearchResult exposing (SearchResult)
import Model.ColorPalette as ColorPalette exposing (ColorPalette)
import Model.ObjectsChange as ObjectsChange exposing (..)
@@ -185,17 +185,27 @@ decodeObject =
|> required "shape" D.string
-decodeSearchResult : Decoder SearchResult
+decodeSearchResult : Decoder (Maybe SearchResult)
decodeSearchResult =
decode
- SearchResult
+ (\maybePersonId maybeObjectAndFloorId ->
+ case (maybePersonId, maybeObjectAndFloorId) of
+ (_, Just (object, floorId)) ->
+ Just <| SearchResult.Object object floorId
+
+ (Just personId, Nothing) ->
+ Just <| SearchResult.MissingPerson personId
+
+ _ ->
+ Nothing
+ )
|> optional' "personId" D.string
|> optional' "objectAndFloorId" (D.tuple2 (,) decodeObject D.string)
decodeSearchResults : Decoder (List SearchResult)
decodeSearchResults =
- D.list decodeSearchResult
+ D.map (List.filterMap identity) (D.list decodeSearchResult)
decodeFloor : Decoder Floor
@@ -1,19 +1,31 @@
module Model.SearchResult exposing (..)
import Dict exposing (Dict)
-import Model.Object exposing (Object)
+import Model.Object as Object exposing (Object)
import Model.Person exposing (Person)
type alias Id = String
+type alias FloorId = String
+type alias PersonId = String
+type alias PersonName = String
-type alias SearchResult =
- { personId : Maybe Id
- , objectAndFloorId : Maybe (Object, Id)
- } -- TODO no (nothing, nothing) pattern!
+type SearchResult
+ = Object Object FloorId
+ | MissingPerson PersonId
type alias SearchResultsForOnePost =
- (Maybe String, List SearchResult)
+ (Maybe PersonName, List SearchResult)
+
+
+getPersonId : SearchResult -> Maybe Id
+getPersonId result =
+ case result of
+ Object o _ ->
+ Object.relatedPerson o
+
+ MissingPerson personId ->
+ Just personId
groupByPostAndReorder : Maybe String -> Dict String Person -> List SearchResult -> List SearchResultsForOnePost
@@ -25,8 +37,8 @@ groupByPostAndReorder thisFloorId personInfo results =
groupByPost : Dict String Person -> List SearchResult -> List SearchResultsForOnePost
groupByPost personInfo results =
Dict.values <|
- groupBy (\r ->
- case r.personId of
+ groupBy (\result ->
+ case getPersonId result of
Just id ->
case Dict.get id personInfo of
Just person ->
@@ -61,20 +73,48 @@ reorderResults : Maybe String -> List SearchResult -> List SearchResult
reorderResults thisFloorId results =
let
(inThisFloor, inOtherFloor, missing) =
- List.foldl (\({ personId, objectAndFloorId } as result) (this, other, miss) ->
- case objectAndFloorId of
- Just (_, fid) ->
- if Just fid == thisFloorId then
+ List.foldl (\result (this, other, miss) ->
+ case result of
+ Object _ floorId ->
+ if Just floorId == thisFloorId then
(result :: this, other, miss)
else
(this, result :: other, miss)
- Nothing ->
+ MissingPerson _ ->
(this, other, result :: miss)
+
) ([], [], []) results
in
inThisFloor ++ inOtherFloor ++ missing
-mergeObjectInfo : List Object -> List SearchResult -> List SearchResult
-mergeObjectInfo objects results = results
+mergeObjectInfo : String -> List Object -> List SearchResult -> List SearchResult
+mergeObjectInfo currentFloorId objects results =
+ List.concatMap (\result ->
+ case result of
+ Object object floorId ->
+ if floorId == currentFloorId then
+ case List.filter (\o -> Object.idOf object == Object.idOf o) objects of
+ [] ->
+ case Object.relatedPerson object of
+ Just personId ->
+ [ MissingPerson personId ]
+
+ Nothing ->
+ []
+
+ _ ->
+ [ result ]
+ else
+ [ result ]
+
+ MissingPerson personId ->
+ case List.filter (\object -> Object.relatedPerson object == Just personId) objects of
+ [] ->
+ [ result ]
+
+ objects ->
+ List.map (\object -> Object object currentFloorId) objects
+
+ ) results
@@ -7,7 +7,7 @@ import Html.Events exposing (..)
import InlineHover exposing (hover)
-import Model.Object exposing (..)
+import Model.Object as Object exposing (..)
import Model.Floor exposing (Floor, FloorBase)
import Model.FloorInfo as FloorInfo exposing (FloorInfo(..))
import Model.Person exposing (Person)
@@ -104,27 +104,27 @@ viewListForOnePost model (maybePostName, results) =
toItemViewModel : Language -> Dict String FloorBase -> Dict String Person -> Maybe Id -> SearchResult -> Maybe Item
toItemViewModel lang floorsInfo personInfo currentlyFocusedObjectId result =
- case (result.objectAndFloorId, result.personId) of
- (Just (object, fid), Just personId) ->
- case (Dict.get fid floorsInfo, Dict.get personId personInfo) of
- (Just info, Just person) ->
- Just (SearchResultItemView.Object (idOf object) (nameOf object) info.name (Just person.name) (Just (idOf object) == currentlyFocusedObjectId))
-
- _ ->
- Nothing
-
- (Just (object, floorId), _) ->
+ case result of
+ SearchResult.Object object floorId ->
case Dict.get floorId floorsInfo of
Just info ->
- Just (SearchResultItemView.Object (idOf object) (nameOf object) info.name Nothing (Just (idOf object) == currentlyFocusedObjectId))
+ let
+ objectIsFocused =
+ Just (idOf object) == currentlyFocusedObjectId
+
+ maybePersonName =
+ Object.relatedPerson object
+ |> (flip Maybe.andThen) (\personId -> Dict.get personId personInfo)
+ |> (flip Maybe.andThen) (\person -> Just person.name)
+
+ _ = Debug.log "Object" ((idOf object), (nameOf object), info.name)
+ in
+ Just (SearchResultItemView.Object (idOf object) (nameOf object) info.name maybePersonName objectIsFocused)
_ ->
Nothing
- (Nothing, Just personId) ->
+ SearchResult.MissingPerson personId ->
case Dict.get personId personInfo of
Just person -> Just (SearchResultItemView.MissingPerson personId person.name)
Nothing -> Nothing
-
- _ ->
- Nothing
@@ -1189,42 +1189,37 @@ update removeToken setSelectionStart msg model =
GotSearchResult results ->
let
regesterPersonCmd =
- Cmd.batch <|
- List.filterMap (\r ->
- case r.personId of
- Just id -> Just (regesterPersonIfNotCached model.apiConfig model.personInfo id)
- Nothing -> Nothing
- ) results
+ results
+ |> List.filterMap SearchResult.getPersonId
+ |> List.map (regesterPersonIfNotCached model.apiConfig model.personInfo)
+ |> Cmd.batch
selectedResult =
case results of
- { objectAndFloorId } :: [] ->
- case objectAndFloorId of
- Just (object, fid) ->
- Just (idOf object)
+ SearchResult.Object object floorId :: [] ->
+ Just (idOf object)
- Nothing ->
- Nothing
-
- _ -> Nothing
+ _ ->
+ Nothing
- searchResult = Just results
+ searchResult =
+ Just results
in
{ model |
searchResult = searchResult
, selectedResult = selectedResult
} ! [ regesterPersonCmd ]
- SelectSearchResult { personId, objectAndFloorId } ->
+ SelectSearchResult result ->
let
(newModel, cmd1) =
- case objectAndFloorId of
- Just (obj, floorId) ->
+ case result of
+ SearchResult.Object object floorId ->
let
model' =
Model.adjustOffset
{ model |
- selectedResult = Just (idOf obj)
+ selectedResult = Just (idOf object)
}
requestPrivateFloors =
@@ -1235,15 +1230,15 @@ update removeToken setSelectionStart msg model =
, Navigation.modifyUrl (URL.serialize model')
]
- Nothing ->
+ _ ->
(model, Cmd.none)
- cmd2 =
- case personId of
- Just id -> regesterPersonIfNotCached model.apiConfig model.personInfo id
- Nothing -> Cmd.none
+ regesterPersonCmd =
+ SearchResult.getPersonId result
+ |> Maybe.map (regesterPersonIfNotCached model.apiConfig model.personInfo)
+ |> Maybe.withDefault Cmd.none
in
- newModel ! [cmd1, cmd2]
+ newModel ! [ cmd1, regesterPersonCmd ]
StartDraggingFromMissingPerson personId personName ->
let
@@ -1662,7 +1657,7 @@ updateOnFinishStamp' prototypes model floor =
searchResult =
model.searchResult
- |> (Maybe.map (SearchResult.mergeObjectInfo (EditingFloor.present newFloor).objects))
+ |> Maybe.map (SearchResult.mergeObjectInfo (EditingFloor.present newFloor).id (EditingFloor.present newFloor).objects)
in
(({ model |
seed = newSeed

0 comments on commit 7c32436

Please sign in to comment.