Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.

create unix timestamp and store it in db #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion WebApi/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public PlayerController(IDatabase db, INearByFinder nearByFinder, IDirectionCalc
[HttpPost("{id}/location")]
public ActionResult<LocationUpdateResponseDTO> UpdateLocation(string id, [FromBody] LocationUpdateDTO pos)
{
var newPosition = new Location { lat = pos.Lat, lon = pos.Long, tracked = pos.Tracked, at_home = pos.at_home };
Location newPosition = CreateLocationFromDTO(pos);
if (db.Contains(id))
{
db.Update(id, newPosition);
Expand All @@ -73,6 +73,18 @@ public ActionResult<LocationUpdateResponseDTO> UpdateLocation(string id, [FromBo
};
}

private static Location CreateLocationFromDTO(LocationUpdateDTO pos)
{
return new Location
{
lat = pos.Lat,
lon = pos.Long,
tracked = pos.Tracked,
at_home = pos.at_home,
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
};
}

private LocationUpdateResponseNearbyPlayerDTO CreateNearbyPlayerDTO(Location me, Location other)
{
var latDistance = me.lat - other.lat;
Expand Down
12 changes: 12 additions & 0 deletions WebApi/Dependencies/MongoDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class PlayerModel
public bool currentTracked;
public bool previousTracked;
public bool atHome;
public long currentTimestamp;
public long previousTimestamp;
}

public class MongoDatabase : IDatabase, INearByFinder
Expand Down Expand Up @@ -66,6 +68,8 @@ public void Create(string id, Location location)
atHome = location.at_home,
currentTracked = location.tracked,
previousTracked = location.tracked,
currentTimestamp = location.timestamp,
previousTimestamp = location.timestamp,
});
}

Expand All @@ -85,10 +89,18 @@ public void Update(string id, Location location)
"$set",
new BsonDocument(new BsonElement("previousTracked", "$currentTracked"))
)),
new BsonDocument(new BsonElement(
"$set",
new BsonDocument(new BsonElement("previousTimestamp", "$currentTimestamp"))
)),
new BsonDocument(new BsonElement(
"$set",
new BsonDocument(new BsonElement("currentTracked", location.tracked))
)),
new BsonDocument(new BsonElement(
"$set",
new BsonDocument(new BsonElement("currentTimestamp", location.timestamp))
)),
new BsonDocument(new BsonElement(
"$set",
new BsonDocument(new BsonElement("currentLocation", GeoJson.Point(GeoJson.Geographic(location.lon, location.lat)).ToBsonDocument()))
Expand Down
5 changes: 5 additions & 0 deletions WebApi/Entities/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public class Location
public double lon;
public bool tracked;
public bool at_home;

/// <summary>
/// represents the number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z
/// </summary>
public long timestamp;
}
}