{"action":"created","comment":{"url":"https://api.github.com/repos/roszman/UmbracoFood/pulls/comments/50828559","id":50828559,"diff_hunk":"@@ -4,61 +4,122 @@\n using Umbraco.Core.Persistence;\n using UmbracoFood.Core.Interfaces;\n using UmbracoFood.Core.Models;\n+using UmbracoFood.Infrastructure.Mapping;\n using UmbracoFood.Infrastructure.Models.POCO;\n-\n+using System;\r\n+\r\n namespace UmbracoFood.Infrastructure.Repositories\n {\n     public class OrderRepository : IOrderRepository\n     {\n         private readonly IDatabaseProvider _databaseProvider;\n+        private readonly IModelMapper<Order, OrderPoco> _mapper;\n         private UmbracoDatabase _db;\n \n-        public OrderRepository(IDatabaseProvider databaseProvider)\n+        public OrderRepository(IDatabaseProvider databaseProvider, IModelMapper<Order, OrderPoco> mapper )\n         {\n             _databaseProvider = databaseProvider;\n+            _mapper = mapper;\n             _db = _databaseProvider.Db;\n         }\n \n         public int AddOrder(Order order)\r\n         {\r\n-            var id = _db.Insert(\"Orders\", \"Id\", Mapper.Map<Order, OrderPoco>(order));\r\n-            return decimal.ToInt32((decimal)id);\r\n+            try\r\n+            {\r\n+                _db.BeginTransaction();\r\n+                // Do transacted updates here\r\n+                var poco = _mapper.MapToPoco(order);\r\n+                var insertResult = _db.Insert(\"Orders\", \"Id\", poco);\r\n+                var id = Convert.ToInt32(insertResult);\r\n+                foreach(var orderedMeal in poco.OrderedMeals)\r\n+                {\r\n+                    orderedMeal.OrderId = id;\r\n+                    _db.Insert(\"OrderedMeals\", \"Id\", orderedMeal);\r\n+                }\r\n+\r\n+                // Commit\r\n+                _db.CompleteTransaction();\r\n+                return id;\r\n+            }\r\n+            catch(Exception)\r\n+            {\r\n+                _db.AbortTransaction();\r\n+                throw;\r\n+            }\r\n         }\r\n \r\n         public void EditOrder(Order order)\r\n         {\r\n-            var updateOrder = _db.SingleOrDefault<OrderPoco>(\"SELECT * FROM Orders WHERE Id = @0\", order.Id);\r\n-            if (updateOrder == null)\r\n+            var orderPoco = _mapper.MapToPoco(order);\r\n+            try\r\n             {\r\n-                throw new KeyNotFoundException(\"Order has not been found.\");\r\n-            }\r\n+                _db.BeginTransaction();\r\n \r\n-            _db.Update(\"Order\", \"Id\", Mapper.Map(order, updateOrder));\r\n+                _db.Execute(\"DELETE FROM OrderedMeals Where OrderId = @0\", orderPoco.Id);\r\n+                foreach (var orderedMealPoco in orderPoco.OrderedMeals)\r\n+                {\r\n+                    orderedMealPoco.OrderId = orderPoco.Id;\r\n+                    _db.Insert(\"OrderedMeals\", \"Id\", orderedMealPoco);\r\n+                }\r\n+                _db.Update(\"Orders\", \"Id\", orderPoco);\r\n+                _db.CompleteTransaction();\r\n+            }\r\n+            catch (Exception)\r\n+            {\r\n+                _db.AbortTransaction();\r\n+                throw;\r\n+            }\r\n         }\r\n \r\n         public void RemoveOrder(int id)\r\n         {\r\n-            _db.Execute(\"DELETE FROM Orders WHERE Id = @0\", id);\r\n+\r\n+            try\r\n+            {\r\n+                _db.BeginTransaction();\r\n+\r\n+                _db.Execute(\"DELETE FROM OrderedMeals WHERE OrderId = @0\", id);\r\n+                _db.Execute(\"DELETE FROM Orders WHERE Id = @0\", id);\r\n+\r\n+                _db.CompleteTransaction();\r\n+            }\r\n+            catch (Exception)\r\n+            {\r\n+                _db.AbortTransaction();\r\n+                throw;\r\n+            }\r\n         }\r\n \r\n         public Order GetOrder(int id)\r\n         {\r\n-            var oreder = _db.SingleOrDefault<OrderPoco>(\"SELECT * FROM Orders WHERE Id = @0\", id);\r\n-            if (oreder == null)\r\n+            var order = _db.Fetch<OrderPoco, OrderedMealPoco, StatusPoco, RestaurantPoco, OrderPoco>(\r","path":"UmbracoFood.Infrastructure/Repositories/OrderRepository.cs","position":105,"original_position":105,"commit_id":"684d3e62016be4bdabcf95f0a09682fb98101b6f","original_commit_id":"684d3e62016be4bdabcf95f0a09682fb98101b6f","user":{"login":"mroclawskipgs","id":16589675,"avatar_url":"https://avatars.githubusercontent.com/u/16589675?v=3","gravatar_id":"","url":"https://api.github.com/users/mroclawskipgs","html_url":"https://github.com/mroclawskipgs","followers_url":"https://api.github.com/users/mroclawskipgs/followers","following_url":"https://api.github.com/users/mroclawskipgs/following{/other_user}","gists_url":"https://api.github.com/users/mroclawskipgs/gists{/gist_id}","starred_url":"https://api.github.com/users/mroclawskipgs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mroclawskipgs/subscriptions","organizations_url":"https://api.github.com/users/mroclawskipgs/orgs","repos_url":"https://api.github.com/users/mroclawskipgs/repos","events_url":"https://api.github.com/users/mroclawskipgs/events{/privacy}","received_events_url":"https://api.github.com/users/mroclawskipgs/received_events","type":"User","site_admin":false},"body":"Nie można użyć _db.FirstOrDefault<> ?","created_at":"2016-01-26T12:30:30Z","updated_at":"2016-01-26T12:30:30Z","html_url":"https://github.com/roszman/UmbracoFood/pull/25#discussion_r50828559","pull_request_url":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25","_links":{"self":{"href":"https://api.github.com/repos/roszman/UmbracoFood/pulls/comments/50828559"},"html":{"href":"https://github.com/roszman/UmbracoFood/pull/25#discussion_r50828559"},"pull_request":{"href":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25"}}},"pull_request":{"url":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25","id":57195356,"html_url":"https://github.com/roszman/UmbracoFood/pull/25","diff_url":"https://github.com/roszman/UmbracoFood/pull/25.diff","patch_url":"https://github.com/roszman/UmbracoFood/pull/25.patch","issue_url":"https://api.github.com/repos/roszman/UmbracoFood/issues/25","number":25,"state":"open","locked":false,"title":"Order repository","user":{"login":"roszman","id":460685,"avatar_url":"https://avatars.githubusercontent.com/u/460685?v=3","gravatar_id":"","url":"https://api.github.com/users/roszman","html_url":"https://github.com/roszman","followers_url":"https://api.github.com/users/roszman/followers","following_url":"https://api.github.com/users/roszman/following{/other_user}","gists_url":"https://api.github.com/users/roszman/gists{/gist_id}","starred_url":"https://api.github.com/users/roszman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/roszman/subscriptions","organizations_url":"https://api.github.com/users/roszman/orgs","repos_url":"https://api.github.com/users/roszman/repos","events_url":"https://api.github.com/users/roszman/events{/privacy}","received_events_url":"https://api.github.com/users/roszman/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-01-26T10:52:14Z","updated_at":"2016-01-26T12:30:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3851319de9030ecdcc3908a8cadd3b3f212785c9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25/commits","review_comments_url":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25/comments","review_comment_url":"https://api.github.com/repos/roszman/UmbracoFood/pulls/comments{/number}","comments_url":"https://api.github.com/repos/roszman/UmbracoFood/issues/25/comments","statuses_url":"https://api.github.com/repos/roszman/UmbracoFood/statuses/684d3e62016be4bdabcf95f0a09682fb98101b6f","head":{"label":"roszman:order-repository","ref":"order-repository","sha":"684d3e62016be4bdabcf95f0a09682fb98101b6f","user":{"login":"roszman","id":460685,"avatar_url":"https://avatars.githubusercontent.com/u/460685?v=3","gravatar_id":"","url":"https://api.github.com/users/roszman","html_url":"https://github.com/roszman","followers_url":"https://api.github.com/users/roszman/followers","following_url":"https://api.github.com/users/roszman/following{/other_user}","gists_url":"https://api.github.com/users/roszman/gists{/gist_id}","starred_url":"https://api.github.com/users/roszman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/roszman/subscriptions","organizations_url":"https://api.github.com/users/roszman/orgs","repos_url":"https://api.github.com/users/roszman/repos","events_url":"https://api.github.com/users/roszman/events{/privacy}","received_events_url":"https://api.github.com/users/roszman/received_events","type":"User","site_admin":false},"repo":{"id":49015121,"name":"UmbracoFood","full_name":"roszman/UmbracoFood","owner":{"login":"roszman","id":460685,"avatar_url":"https://avatars.githubusercontent.com/u/460685?v=3","gravatar_id":"","url":"https://api.github.com/users/roszman","html_url":"https://github.com/roszman","followers_url":"https://api.github.com/users/roszman/followers","following_url":"https://api.github.com/users/roszman/following{/other_user}","gists_url":"https://api.github.com/users/roszman/gists{/gist_id}","starred_url":"https://api.github.com/users/roszman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/roszman/subscriptions","organizations_url":"https://api.github.com/users/roszman/orgs","repos_url":"https://api.github.com/users/roszman/repos","events_url":"https://api.github.com/users/roszman/events{/privacy}","received_events_url":"https://api.github.com/users/roszman/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/roszman/UmbracoFood","description":"","fork":false,"url":"https://api.github.com/repos/roszman/UmbracoFood","forks_url":"https://api.github.com/repos/roszman/UmbracoFood/forks","keys_url":"https://api.github.com/repos/roszman/UmbracoFood/keys{/key_id}","collaborators_url":"https://api.github.com/repos/roszman/UmbracoFood/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/roszman/UmbracoFood/teams","hooks_url":"https://api.github.com/repos/roszman/UmbracoFood/hooks","issue_events_url":"https://api.github.com/repos/roszman/UmbracoFood/issues/events{/number}","events_url":"https://api.github.com/repos/roszman/UmbracoFood/events","assignees_url":"https://api.github.com/repos/roszman/UmbracoFood/assignees{/user}","branches_url":"https://api.github.com/repos/roszman/UmbracoFood/branches{/branch}","tags_url":"https://api.github.com/repos/roszman/UmbracoFood/tags","blobs_url":"https://api.github.com/repos/roszman/UmbracoFood/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/roszman/UmbracoFood/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/roszman/UmbracoFood/git/refs{/sha}","trees_url":"https://api.github.com/repos/roszman/UmbracoFood/git/trees{/sha}","statuses_url":"https://api.github.com/repos/roszman/UmbracoFood/statuses/{sha}","languages_url":"https://api.github.com/repos/roszman/UmbracoFood/languages","stargazers_url":"https://api.github.com/repos/roszman/UmbracoFood/stargazers","contributors_url":"https://api.github.com/repos/roszman/UmbracoFood/contributors","subscribers_url":"https://api.github.com/repos/roszman/UmbracoFood/subscribers","subscription_url":"https://api.github.com/repos/roszman/UmbracoFood/subscription","commits_url":"https://api.github.com/repos/roszman/UmbracoFood/commits{/sha}","git_commits_url":"https://api.github.com/repos/roszman/UmbracoFood/git/commits{/sha}","comments_url":"https://api.github.com/repos/roszman/UmbracoFood/comments{/number}","issue_comment_url":"https://api.github.com/repos/roszman/UmbracoFood/issues/comments{/number}","contents_url":"https://api.github.com/repos/roszman/UmbracoFood/contents/{+path}","compare_url":"https://api.github.com/repos/roszman/UmbracoFood/compare/{base}...{head}","merges_url":"https://api.github.com/repos/roszman/UmbracoFood/merges","archive_url":"https://api.github.com/repos/roszman/UmbracoFood/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/roszman/UmbracoFood/downloads","issues_url":"https://api.github.com/repos/roszman/UmbracoFood/issues{/number}","pulls_url":"https://api.github.com/repos/roszman/UmbracoFood/pulls{/number}","milestones_url":"https://api.github.com/repos/roszman/UmbracoFood/milestones{/number}","notifications_url":"https://api.github.com/repos/roszman/UmbracoFood/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/roszman/UmbracoFood/labels{/name}","releases_url":"https://api.github.com/repos/roszman/UmbracoFood/releases{/id}","deployments_url":"https://api.github.com/repos/roszman/UmbracoFood/deployments","created_at":"2016-01-04T18:28:08Z","updated_at":"2016-01-11T09:24:57Z","pushed_at":"2016-01-26T10:52:14Z","git_url":"git://github.com/roszman/UmbracoFood.git","ssh_url":"git@github.com:roszman/UmbracoFood.git","clone_url":"https://github.com/roszman/UmbracoFood.git","svn_url":"https://github.com/roszman/UmbracoFood","homepage":null,"size":9336,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"roszman:master","ref":"master","sha":"e8c642e2c0c7d9c7c86a7435a8ea5f5114dcbce4","user":{"login":"roszman","id":460685,"avatar_url":"https://avatars.githubusercontent.com/u/460685?v=3","gravatar_id":"","url":"https://api.github.com/users/roszman","html_url":"https://github.com/roszman","followers_url":"https://api.github.com/users/roszman/followers","following_url":"https://api.github.com/users/roszman/following{/other_user}","gists_url":"https://api.github.com/users/roszman/gists{/gist_id}","starred_url":"https://api.github.com/users/roszman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/roszman/subscriptions","organizations_url":"https://api.github.com/users/roszman/orgs","repos_url":"https://api.github.com/users/roszman/repos","events_url":"https://api.github.com/users/roszman/events{/privacy}","received_events_url":"https://api.github.com/users/roszman/received_events","type":"User","site_admin":false},"repo":{"id":49015121,"name":"UmbracoFood","full_name":"roszman/UmbracoFood","owner":{"login":"roszman","id":460685,"avatar_url":"https://avatars.githubusercontent.com/u/460685?v=3","gravatar_id":"","url":"https://api.github.com/users/roszman","html_url":"https://github.com/roszman","followers_url":"https://api.github.com/users/roszman/followers","following_url":"https://api.github.com/users/roszman/following{/other_user}","gists_url":"https://api.github.com/users/roszman/gists{/gist_id}","starred_url":"https://api.github.com/users/roszman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/roszman/subscriptions","organizations_url":"https://api.github.com/users/roszman/orgs","repos_url":"https://api.github.com/users/roszman/repos","events_url":"https://api.github.com/users/roszman/events{/privacy}","received_events_url":"https://api.github.com/users/roszman/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/roszman/UmbracoFood","description":"","fork":false,"url":"https://api.github.com/repos/roszman/UmbracoFood","forks_url":"https://api.github.com/repos/roszman/UmbracoFood/forks","keys_url":"https://api.github.com/repos/roszman/UmbracoFood/keys{/key_id}","collaborators_url":"https://api.github.com/repos/roszman/UmbracoFood/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/roszman/UmbracoFood/teams","hooks_url":"https://api.github.com/repos/roszman/UmbracoFood/hooks","issue_events_url":"https://api.github.com/repos/roszman/UmbracoFood/issues/events{/number}","events_url":"https://api.github.com/repos/roszman/UmbracoFood/events","assignees_url":"https://api.github.com/repos/roszman/UmbracoFood/assignees{/user}","branches_url":"https://api.github.com/repos/roszman/UmbracoFood/branches{/branch}","tags_url":"https://api.github.com/repos/roszman/UmbracoFood/tags","blobs_url":"https://api.github.com/repos/roszman/UmbracoFood/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/roszman/UmbracoFood/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/roszman/UmbracoFood/git/refs{/sha}","trees_url":"https://api.github.com/repos/roszman/UmbracoFood/git/trees{/sha}","statuses_url":"https://api.github.com/repos/roszman/UmbracoFood/statuses/{sha}","languages_url":"https://api.github.com/repos/roszman/UmbracoFood/languages","stargazers_url":"https://api.github.com/repos/roszman/UmbracoFood/stargazers","contributors_url":"https://api.github.com/repos/roszman/UmbracoFood/contributors","subscribers_url":"https://api.github.com/repos/roszman/UmbracoFood/subscribers","subscription_url":"https://api.github.com/repos/roszman/UmbracoFood/subscription","commits_url":"https://api.github.com/repos/roszman/UmbracoFood/commits{/sha}","git_commits_url":"https://api.github.com/repos/roszman/UmbracoFood/git/commits{/sha}","comments_url":"https://api.github.com/repos/roszman/UmbracoFood/comments{/number}","issue_comment_url":"https://api.github.com/repos/roszman/UmbracoFood/issues/comments{/number}","contents_url":"https://api.github.com/repos/roszman/UmbracoFood/contents/{+path}","compare_url":"https://api.github.com/repos/roszman/UmbracoFood/compare/{base}...{head}","merges_url":"https://api.github.com/repos/roszman/UmbracoFood/merges","archive_url":"https://api.github.com/repos/roszman/UmbracoFood/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/roszman/UmbracoFood/downloads","issues_url":"https://api.github.com/repos/roszman/UmbracoFood/issues{/number}","pulls_url":"https://api.github.com/repos/roszman/UmbracoFood/pulls{/number}","milestones_url":"https://api.github.com/repos/roszman/UmbracoFood/milestones{/number}","notifications_url":"https://api.github.com/repos/roszman/UmbracoFood/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/roszman/UmbracoFood/labels{/name}","releases_url":"https://api.github.com/repos/roszman/UmbracoFood/releases{/id}","deployments_url":"https://api.github.com/repos/roszman/UmbracoFood/deployments","created_at":"2016-01-04T18:28:08Z","updated_at":"2016-01-11T09:24:57Z","pushed_at":"2016-01-26T10:52:14Z","git_url":"git://github.com/roszman/UmbracoFood.git","ssh_url":"git@github.com:roszman/UmbracoFood.git","clone_url":"https://github.com/roszman/UmbracoFood.git","svn_url":"https://github.com/roszman/UmbracoFood","homepage":null,"size":9336,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25"},"html":{"href":"https://github.com/roszman/UmbracoFood/pull/25"},"issue":{"href":"https://api.github.com/repos/roszman/UmbracoFood/issues/25"},"comments":{"href":"https://api.github.com/repos/roszman/UmbracoFood/issues/25/comments"},"review_comments":{"href":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25/comments"},"review_comment":{"href":"https://api.github.com/repos/roszman/UmbracoFood/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/roszman/UmbracoFood/pulls/25/commits"},"statuses":{"href":"https://api.github.com/repos/roszman/UmbracoFood/statuses/684d3e62016be4bdabcf95f0a09682fb98101b6f"}}}}