1
+ var bookWishlistAppControllers = angular . module ( 'bookWishlistAppControllers' , [
2
+ 'bookWishlistAppServices'
3
+ ] ) ;
4
+
5
+ bookWishlistAppControllers . controller ( 'LoginController' , [ '$scope' , '$location' , 'userService' , function ( $scope , $location , userService ) {
6
+
7
+ $scope . login = function ( ) {
8
+ userService . login (
9
+ $scope . email , $scope . password ,
10
+ function ( response ) {
11
+ $location . path ( '/' ) ;
12
+ } ,
13
+ function ( response ) {
14
+ alert ( 'Something went wrong with the login process. Try again later!' ) ;
15
+ }
16
+ ) ;
17
+ }
18
+
19
+ $scope . email = '' ;
20
+ $scope . password = '' ;
21
+
22
+ if ( userService . checkIfLoggedIn ( ) )
23
+ $location . path ( '/' ) ;
24
+
25
+ } ] ) ;
26
+
27
+ bookWishlistAppControllers . controller ( 'SignupController' , [ '$scope' , '$location' , 'userService' , function ( $scope , $location , userService ) {
28
+
29
+ $scope . signup = function ( ) {
30
+ userService . signup (
31
+ $scope . name , $scope . email , $scope . password ,
32
+ function ( response ) {
33
+ alert ( 'Great! You are now signed in! Welcome, ' + $scope . name + '!' ) ;
34
+ $location . path ( '/' ) ;
35
+ } ,
36
+ function ( response ) {
37
+ alert ( 'Something went wrong with the signup process. Try again later.' ) ;
38
+ }
39
+ ) ;
40
+ }
41
+
42
+ $scope . name = '' ;
43
+ $scope . email = '' ;
44
+ $scope . password = '' ;
45
+
46
+ if ( userService . checkIfLoggedIn ( ) )
47
+ $location . path ( '/' ) ;
48
+
49
+ } ] ) ;
50
+
51
+ bookWishlistAppControllers . controller ( 'MainController' , [ '$scope' , '$location' , 'userService' , 'bookService' , function ( $scope , $location , userService , bookService ) {
52
+
53
+ $scope . logout = function ( ) {
54
+ userService . logout ( ) ;
55
+ $location . path ( '/login' ) ;
56
+ }
57
+
58
+ $scope . create = function ( ) {
59
+
60
+ bookService . create ( {
61
+ title : $scope . currentBookTitle ,
62
+ author_name : $scope . currentBookAuthorName ,
63
+ pages_count : $scope . currentBookPagesCount
64
+ } , function ( ) {
65
+
66
+ $ ( '#addBookModal' ) . modal ( 'toggle' ) ;
67
+ $scope . currentBookReset ( ) ;
68
+ $scope . refresh ( ) ;
69
+
70
+ } , function ( ) {
71
+
72
+ alert ( 'Some errors occurred while communicating with the service. Try again later.' ) ;
73
+
74
+ } ) ;
75
+
76
+ }
77
+
78
+ $scope . refresh = function ( ) {
79
+
80
+ bookService . getAll ( function ( response ) {
81
+
82
+ $scope . books = response ;
83
+ console . log ( $scope . books ) ;
84
+
85
+
86
+ } , function ( ) {
87
+
88
+ alert ( 'Some errors occurred while communicating with the service. Try again later.' ) ;
89
+
90
+ } ) ;
91
+
92
+ }
93
+
94
+ $scope . load = function ( bookId ) {
95
+
96
+ bookService . getById ( bookId , function ( response ) {
97
+
98
+ $scope . currentBookId = response . book . id ;
99
+ $scope . currentBookTitle = response . book . title ;
100
+ $scope . currentBookAuthorName = response . book . author_name ;
101
+ $scope . currentBookPagesCount = response . book . pages_count ;
102
+
103
+ $ ( '#updateBookModal' ) . modal ( 'toggle' ) ;
104
+
105
+ } , function ( ) {
106
+
107
+ alert ( 'Some errors occurred while communicating with the service. Try again later.' ) ;
108
+
109
+ } ) ;
110
+
111
+ }
112
+
113
+ $scope . update = function ( ) {
114
+
115
+ bookService . update (
116
+ $scope . currentBookId ,
117
+ {
118
+ title : $scope . currentBookTitle ,
119
+ author_name : $scope . currentBookAuthorName ,
120
+ pages_count : $scope . currentBookPagesCount
121
+ } ,
122
+ function ( response ) {
123
+
124
+ $ ( '#updateBookModal' ) . modal ( 'toggle' ) ;
125
+ $scope . currentBookReset ( ) ;
126
+ $scope . refresh ( ) ;
127
+
128
+ } , function ( response ) {
129
+ alert ( 'Some errors occurred while communicating with the service. Try again later.' ) ;
130
+ }
131
+ ) ;
132
+
133
+ }
134
+
135
+ $scope . remove = function ( bookId ) {
136
+
137
+ if ( confirm ( 'Are you sure to remove this book from your wishlist?' ) ) {
138
+ bookService . remove ( bookId , function ( ) {
139
+
140
+ alert ( 'Book removed successfully.' ) ;
141
+
142
+ } , function ( ) {
143
+
144
+ alert ( 'Some errors occurred while communicating with the service. Try again later.' ) ;
145
+
146
+ } ) ;
147
+ }
148
+
149
+ }
150
+
151
+ $scope . currentBookReset = function ( ) {
152
+ $scope . currentBookTitle = '' ;
153
+ $scope . currentBookAuthorName = '' ;
154
+ $scope . currentBookPagesCount = '' ;
155
+ $scope . currentBookId = '' ;
156
+ }
157
+
158
+ if ( ! userService . checkIfLoggedIn ( ) )
159
+ $location . path ( '/login' ) ;
160
+
161
+ $scope . books = [ ] ;
162
+
163
+ $scope . currentBookReset ( ) ;
164
+ $scope . refresh ( ) ;
165
+
166
+ } ] ) ;
0 commit comments