{"action":"created","comment":{"url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/comments/239860454","pull_request_review_id":182761845,"id":239860454,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzOTg2MDQ1NA==","diff_hunk":"@@ -0,0 +1,134 @@\n+package mw.zadanie.parkingmeter.service;\n+\n+import mw.zadanie.parkingmeter.model.DriverType;\n+import mw.zadanie.parkingmeter.model.ParkingSession;\n+import mw.zadanie.parkingmeter.repository.ParkingSessionRepository;\n+import org.springframework.beans.factory.annotation.Autowired;\n+import org.springframework.stereotype.Service;\n+\n+import java.math.BigDecimal;\n+import java.math.RoundingMode;\n+import java.text.ParseException;\n+import java.text.SimpleDateFormat;\n+import java.util.Date;\n+import java.util.List;\n+import java.util.concurrent.TimeUnit;\n+\n+@Service\n+public class CostCalculationServiceImpl implements CostCalculationService {\n+\n+    @Autowired\n+    private ParkingSessionRepository sessionRepository;\n+\n+\n+    private static double roundToTwoDecimalPlaces(double value) {\n+        BigDecimal bd = new BigDecimal(Double.toString(value));\n+        bd = bd.setScale(2, RoundingMode.HALF_DOWN);\n+        return bd.doubleValue();\n+    }\n+\n+    @Override\n+    public Double calculateCostForSession(Long sessionId) {\n+        ParkingSession session = sessionRepository.findById(sessionId).orElse(null);\n+        return session != null ? calculateCost(session) : null;\n+    }\n+\n+    @Override\n+    public Double calculateEarningsForDay(String dateString) {\n+        SimpleDateFormat dateFormat = new SimpleDateFormat(\"dd-MM-yyyy\");\n+\n+        Date periodStart = null;\n+        try {\n+            periodStart = dateFormat.parse(dateString);\n+        } catch (ParseException e) {\n+            // todo: properly handle\n+            e.printStackTrace();\n+        }\n+\n+        double totalEarnings = 0;\n+        List<ParkingSession> sessions = sessionRepository.findAll();\n+\n+        if (sessions.isEmpty() || periodStart == null) {\n+            return totalEarnings;\n+        }\n+\n+        int dayInMs = 86400000;\n+        for (ParkingSession session: sessions) {\n+            if (session.getEndTime() != null\n+                    && session.getEndTime().getTime() > periodStart.getTime()\n+                    && session.getEndTime().getTime() < periodStart.getTime() + dayInMs) {\n+                totalEarnings += calculateCost(session);\n+            }\n+        }\n+        return totalEarnings;\n+    }\n+\n+    @Override\n+    public Double calculateTotalEarnings() {\n+        double totalEarnings = 0;\n+\n+        List<ParkingSession> sessions = sessionRepository.findAll();\n+        for (ParkingSession s: sessions) {\n+            totalEarnings += calculateCost(s);\n+        }\n+        return totalEarnings;\n+    }\n+\n+    private double calculateCost(ParkingSession session) {\n+        long startTime = session.getStartTime().getTime();\n+        long endTime = session.getEndTime() != null ? session.getEndTime().getTime() : new Date().getTime();\n+        long timeDiffInMs = endTime - startTime;\n+        long timeDiffInHours = TimeUnit.HOURS.convert(timeDiffInMs, TimeUnit.MILLISECONDS);\n+\n+        DriverType tariff = session.getTariff();\n+\n+        double cost;","path":"src/main/java/mw/zadanie/parkingmeter/service/CostCalculationServiceImpl.java","position":85,"original_position":85,"commit_id":"fe075e3351417e3e4d356b5130dadc5b87f32c9d","original_commit_id":"fe075e3351417e3e4d356b5130dadc5b87f32c9d","user":{"login":"mwisnie","id":35740418,"node_id":"MDQ6VXNlcjM1NzQwNDE4","avatar_url":"https://avatars0.githubusercontent.com/u/35740418?v=4","gravatar_id":"","url":"https://api.github.com/users/mwisnie","html_url":"https://github.com/mwisnie","followers_url":"https://api.github.com/users/mwisnie/followers","following_url":"https://api.github.com/users/mwisnie/following{/other_user}","gists_url":"https://api.github.com/users/mwisnie/gists{/gist_id}","starred_url":"https://api.github.com/users/mwisnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwisnie/subscriptions","organizations_url":"https://api.github.com/users/mwisnie/orgs","repos_url":"https://api.github.com/users/mwisnie/repos","events_url":"https://api.github.com/users/mwisnie/events{/privacy}","received_events_url":"https://api.github.com/users/mwisnie/received_events","type":"User","site_admin":false},"body":"double cost = (tariff == DriverType.DISABLED) ? calculateCostForDisabled(timeDiffInHours) : calculateCostForRegular(timeDiffInHours);","created_at":"2018-12-07T16:09:04Z","updated_at":"2018-12-07T16:09:04Z","html_url":"https://github.com/mwisnie/parkingMeter/pull/1#discussion_r239860454","pull_request_url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/comments/239860454"},"html":{"href":"https://github.com/mwisnie/parkingMeter/pull/1#discussion_r239860454"},"pull_request":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1"}},"in_reply_to_id":238947289},"pull_request":{"url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1","id":235795411,"node_id":"MDExOlB1bGxSZXF1ZXN0MjM1Nzk1NDEx","html_url":"https://github.com/mwisnie/parkingMeter/pull/1","diff_url":"https://github.com/mwisnie/parkingMeter/pull/1.diff","patch_url":"https://github.com/mwisnie/parkingMeter/pull/1.patch","issue_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues/1","number":1,"state":"open","locked":false,"title":"ParkingMeter REST API Backend","user":{"login":"mwisnie","id":35740418,"node_id":"MDQ6VXNlcjM1NzQwNDE4","avatar_url":"https://avatars0.githubusercontent.com/u/35740418?v=4","gravatar_id":"","url":"https://api.github.com/users/mwisnie","html_url":"https://github.com/mwisnie","followers_url":"https://api.github.com/users/mwisnie/followers","following_url":"https://api.github.com/users/mwisnie/following{/other_user}","gists_url":"https://api.github.com/users/mwisnie/gists{/gist_id}","starred_url":"https://api.github.com/users/mwisnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwisnie/subscriptions","organizations_url":"https://api.github.com/users/mwisnie/orgs","repos_url":"https://api.github.com/users/mwisnie/repos","events_url":"https://api.github.com/users/mwisnie/events{/privacy}","received_events_url":"https://api.github.com/users/mwisnie/received_events","type":"User","site_admin":false},"body":"O projekcie:\r\nhttps://docs.google.com/document/d/18MlJcKbBE6jbfb69YibQVz8B-mqoPk2NvC6l54W5nWM/edit?usp=sharing","created_at":"2018-12-04T12:45:12Z","updated_at":"2018-12-07T16:09:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bd121a8784e5c0c16829487bce59aab1e5d86551","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1/commits","review_comments_url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1/comments","review_comment_url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues/1/comments","statuses_url":"https://api.github.com/repos/mwisnie/parkingMeter/statuses/fe075e3351417e3e4d356b5130dadc5b87f32c9d","head":{"label":"mwisnie:develop","ref":"develop","sha":"fe075e3351417e3e4d356b5130dadc5b87f32c9d","user":{"login":"mwisnie","id":35740418,"node_id":"MDQ6VXNlcjM1NzQwNDE4","avatar_url":"https://avatars0.githubusercontent.com/u/35740418?v=4","gravatar_id":"","url":"https://api.github.com/users/mwisnie","html_url":"https://github.com/mwisnie","followers_url":"https://api.github.com/users/mwisnie/followers","following_url":"https://api.github.com/users/mwisnie/following{/other_user}","gists_url":"https://api.github.com/users/mwisnie/gists{/gist_id}","starred_url":"https://api.github.com/users/mwisnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwisnie/subscriptions","organizations_url":"https://api.github.com/users/mwisnie/orgs","repos_url":"https://api.github.com/users/mwisnie/repos","events_url":"https://api.github.com/users/mwisnie/events{/privacy}","received_events_url":"https://api.github.com/users/mwisnie/received_events","type":"User","site_admin":false},"repo":{"id":157911928,"node_id":"MDEwOlJlcG9zaXRvcnkxNTc5MTE5Mjg=","name":"parkingMeter","full_name":"mwisnie/parkingMeter","private":false,"owner":{"login":"mwisnie","id":35740418,"node_id":"MDQ6VXNlcjM1NzQwNDE4","avatar_url":"https://avatars0.githubusercontent.com/u/35740418?v=4","gravatar_id":"","url":"https://api.github.com/users/mwisnie","html_url":"https://github.com/mwisnie","followers_url":"https://api.github.com/users/mwisnie/followers","following_url":"https://api.github.com/users/mwisnie/following{/other_user}","gists_url":"https://api.github.com/users/mwisnie/gists{/gist_id}","starred_url":"https://api.github.com/users/mwisnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwisnie/subscriptions","organizations_url":"https://api.github.com/users/mwisnie/orgs","repos_url":"https://api.github.com/users/mwisnie/repos","events_url":"https://api.github.com/users/mwisnie/events{/privacy}","received_events_url":"https://api.github.com/users/mwisnie/received_events","type":"User","site_admin":false},"html_url":"https://github.com/mwisnie/parkingMeter","description":"zadanie rekrutacyjne","fork":false,"url":"https://api.github.com/repos/mwisnie/parkingMeter","forks_url":"https://api.github.com/repos/mwisnie/parkingMeter/forks","keys_url":"https://api.github.com/repos/mwisnie/parkingMeter/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mwisnie/parkingMeter/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mwisnie/parkingMeter/teams","hooks_url":"https://api.github.com/repos/mwisnie/parkingMeter/hooks","issue_events_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues/events{/number}","events_url":"https://api.github.com/repos/mwisnie/parkingMeter/events","assignees_url":"https://api.github.com/repos/mwisnie/parkingMeter/assignees{/user}","branches_url":"https://api.github.com/repos/mwisnie/parkingMeter/branches{/branch}","tags_url":"https://api.github.com/repos/mwisnie/parkingMeter/tags","blobs_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/refs{/sha}","trees_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mwisnie/parkingMeter/statuses/{sha}","languages_url":"https://api.github.com/repos/mwisnie/parkingMeter/languages","stargazers_url":"https://api.github.com/repos/mwisnie/parkingMeter/stargazers","contributors_url":"https://api.github.com/repos/mwisnie/parkingMeter/contributors","subscribers_url":"https://api.github.com/repos/mwisnie/parkingMeter/subscribers","subscription_url":"https://api.github.com/repos/mwisnie/parkingMeter/subscription","commits_url":"https://api.github.com/repos/mwisnie/parkingMeter/commits{/sha}","git_commits_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/commits{/sha}","comments_url":"https://api.github.com/repos/mwisnie/parkingMeter/comments{/number}","issue_comment_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues/comments{/number}","contents_url":"https://api.github.com/repos/mwisnie/parkingMeter/contents/{+path}","compare_url":"https://api.github.com/repos/mwisnie/parkingMeter/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mwisnie/parkingMeter/merges","archive_url":"https://api.github.com/repos/mwisnie/parkingMeter/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mwisnie/parkingMeter/downloads","issues_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues{/number}","pulls_url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls{/number}","milestones_url":"https://api.github.com/repos/mwisnie/parkingMeter/milestones{/number}","notifications_url":"https://api.github.com/repos/mwisnie/parkingMeter/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mwisnie/parkingMeter/labels{/name}","releases_url":"https://api.github.com/repos/mwisnie/parkingMeter/releases{/id}","deployments_url":"https://api.github.com/repos/mwisnie/parkingMeter/deployments","created_at":"2018-11-16T19:24:51Z","updated_at":"2018-11-16T19:24:54Z","pushed_at":"2018-12-04T12:45:12Z","git_url":"git://github.com/mwisnie/parkingMeter.git","ssh_url":"git@github.com:mwisnie/parkingMeter.git","clone_url":"https://github.com/mwisnie/parkingMeter.git","svn_url":"https://github.com/mwisnie/parkingMeter","homepage":null,"size":100,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"mwisnie:master","ref":"master","sha":"955498ab3f6012ffecfec9f80acd09832a8bbbf4","user":{"login":"mwisnie","id":35740418,"node_id":"MDQ6VXNlcjM1NzQwNDE4","avatar_url":"https://avatars0.githubusercontent.com/u/35740418?v=4","gravatar_id":"","url":"https://api.github.com/users/mwisnie","html_url":"https://github.com/mwisnie","followers_url":"https://api.github.com/users/mwisnie/followers","following_url":"https://api.github.com/users/mwisnie/following{/other_user}","gists_url":"https://api.github.com/users/mwisnie/gists{/gist_id}","starred_url":"https://api.github.com/users/mwisnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwisnie/subscriptions","organizations_url":"https://api.github.com/users/mwisnie/orgs","repos_url":"https://api.github.com/users/mwisnie/repos","events_url":"https://api.github.com/users/mwisnie/events{/privacy}","received_events_url":"https://api.github.com/users/mwisnie/received_events","type":"User","site_admin":false},"repo":{"id":157911928,"node_id":"MDEwOlJlcG9zaXRvcnkxNTc5MTE5Mjg=","name":"parkingMeter","full_name":"mwisnie/parkingMeter","private":false,"owner":{"login":"mwisnie","id":35740418,"node_id":"MDQ6VXNlcjM1NzQwNDE4","avatar_url":"https://avatars0.githubusercontent.com/u/35740418?v=4","gravatar_id":"","url":"https://api.github.com/users/mwisnie","html_url":"https://github.com/mwisnie","followers_url":"https://api.github.com/users/mwisnie/followers","following_url":"https://api.github.com/users/mwisnie/following{/other_user}","gists_url":"https://api.github.com/users/mwisnie/gists{/gist_id}","starred_url":"https://api.github.com/users/mwisnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwisnie/subscriptions","organizations_url":"https://api.github.com/users/mwisnie/orgs","repos_url":"https://api.github.com/users/mwisnie/repos","events_url":"https://api.github.com/users/mwisnie/events{/privacy}","received_events_url":"https://api.github.com/users/mwisnie/received_events","type":"User","site_admin":false},"html_url":"https://github.com/mwisnie/parkingMeter","description":"zadanie rekrutacyjne","fork":false,"url":"https://api.github.com/repos/mwisnie/parkingMeter","forks_url":"https://api.github.com/repos/mwisnie/parkingMeter/forks","keys_url":"https://api.github.com/repos/mwisnie/parkingMeter/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mwisnie/parkingMeter/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mwisnie/parkingMeter/teams","hooks_url":"https://api.github.com/repos/mwisnie/parkingMeter/hooks","issue_events_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues/events{/number}","events_url":"https://api.github.com/repos/mwisnie/parkingMeter/events","assignees_url":"https://api.github.com/repos/mwisnie/parkingMeter/assignees{/user}","branches_url":"https://api.github.com/repos/mwisnie/parkingMeter/branches{/branch}","tags_url":"https://api.github.com/repos/mwisnie/parkingMeter/tags","blobs_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/refs{/sha}","trees_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mwisnie/parkingMeter/statuses/{sha}","languages_url":"https://api.github.com/repos/mwisnie/parkingMeter/languages","stargazers_url":"https://api.github.com/repos/mwisnie/parkingMeter/stargazers","contributors_url":"https://api.github.com/repos/mwisnie/parkingMeter/contributors","subscribers_url":"https://api.github.com/repos/mwisnie/parkingMeter/subscribers","subscription_url":"https://api.github.com/repos/mwisnie/parkingMeter/subscription","commits_url":"https://api.github.com/repos/mwisnie/parkingMeter/commits{/sha}","git_commits_url":"https://api.github.com/repos/mwisnie/parkingMeter/git/commits{/sha}","comments_url":"https://api.github.com/repos/mwisnie/parkingMeter/comments{/number}","issue_comment_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues/comments{/number}","contents_url":"https://api.github.com/repos/mwisnie/parkingMeter/contents/{+path}","compare_url":"https://api.github.com/repos/mwisnie/parkingMeter/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mwisnie/parkingMeter/merges","archive_url":"https://api.github.com/repos/mwisnie/parkingMeter/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mwisnie/parkingMeter/downloads","issues_url":"https://api.github.com/repos/mwisnie/parkingMeter/issues{/number}","pulls_url":"https://api.github.com/repos/mwisnie/parkingMeter/pulls{/number}","milestones_url":"https://api.github.com/repos/mwisnie/parkingMeter/milestones{/number}","notifications_url":"https://api.github.com/repos/mwisnie/parkingMeter/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mwisnie/parkingMeter/labels{/name}","releases_url":"https://api.github.com/repos/mwisnie/parkingMeter/releases{/id}","deployments_url":"https://api.github.com/repos/mwisnie/parkingMeter/deployments","created_at":"2018-11-16T19:24:51Z","updated_at":"2018-11-16T19:24:54Z","pushed_at":"2018-12-04T12:45:12Z","git_url":"git://github.com/mwisnie/parkingMeter.git","ssh_url":"git@github.com:mwisnie/parkingMeter.git","clone_url":"https://github.com/mwisnie/parkingMeter.git","svn_url":"https://github.com/mwisnie/parkingMeter","homepage":null,"size":100,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1"},"html":{"href":"https://github.com/mwisnie/parkingMeter/pull/1"},"issue":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/issues/1"},"comments":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/mwisnie/parkingMeter/statuses/fe075e3351417e3e4d356b5130dadc5b87f32c9d"}},"author_association":"OWNER"}}
{"actor":{"display_login":"mwisnie"}}