Skip to content

Commit acfcc87

Browse files
committed
Update
1 parent c0d1a81 commit acfcc87

File tree

24 files changed

+20903
-20234
lines changed

24 files changed

+20903
-20234
lines changed

app/components/nav-bar.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import Component from '@glimmer/component';
22
import { inject as service } from '@ember/service';
33
import { action, set, get } from '@ember/object';
4+
import $ from 'jquery';
45

56
export default class NavBarComponent extends Component {
67
@service data;
78
@service userPreference;
89

10+
// constructor(){
11+
// super(...arguments);
12+
// fetch('/libraries/4/books').then(r=>r.json()).then(d=>{
13+
// console.log(d);
14+
// });
15+
16+
// }
17+
918
get libraryReq() {
1019
return this.data.libraryReq;
1120
}

app/models/book.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Model,{belongsTo} from '@ember-data/model';
2+
3+
export default class BookModel extends Model {
4+
@belongsTo('library', { async: true ,inverse: 'library' }) library;
5+
}
6+

app/models/library.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Model, { attr, hasMany } from '@ember-data/model';
2+
3+
export default class LibraryModel extends Model {
4+
@attr name;
5+
@attr location;
6+
@attr timing;
7+
@hasMany('book', { async: true ,inverse: 'book' }) books;
8+
}

app/routes/library/details.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export default class LibraryDetailsRoute extends Route {
77

88
async model({ library_id }) {
99
let url = '/libraries/' + library_id;
10-
let lib = await this.data.getData(url);
10+
let data = await this.data.getData(url);
1111

12-
if (!lib) {
12+
if (!data) {
1313
this.router.transitionTo('not-found', 'notFound');
1414
}
15-
return lib;
15+
return data.library;
1616
}
1717
}
1818

@@ -23,4 +23,4 @@ export default class LibraryDetailsRoute extends Route {
2323
// }
2424
// });
2525

26-
// let lib = fetch(`/libraries/${library_id}`).then((response)=>response.json()).then((r)=> r.data)
26+
// let lib = fetch(`/libraries/${library_id}`).then((response)=>response.json()).then((r)=> r.data)

app/routes/library/details/books/book.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export default class LibraryDetailsBooksBookRoute extends Route {
88
async model({ book_id }) {
99
let library_id = this.paramsFor('library.details').library_id;
1010
let url = `/libraries/${library_id}/books/${book_id}`;
11-
let book = await this.data.getData(url);
12-
if (!book) {
11+
let data = await this.data.getData(url);
12+
if (!data) {
1313
this.router.transitionTo('not-found', 'notfound');
1414
}
15-
return book;
15+
return data.book;
1616
}
1717
}
1818

app/routes/library/details/books/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ export default class LibraryDetailsBooksIndexRoute extends Route {
1414
let s = params.sort;
1515
let library_id = this.paramsFor('library.details').library_id;
1616
let url = '/libraries/' + library_id + '/books';
17-
let books = await this.data.getData(url);
17+
let data = await this.data.getData(url);
1818

19-
return books.sort((a, b) => {
19+
return data.books.sort((a, b) => {
2020
if (s === 'asc') {
21-
return a.attributes.name.localeCompare(b.attributes.name);
21+
return a.name.localeCompare(b.name);
2222
} else if (s === 'desc') {
23-
return b.attributes.name.localeCompare(a.attributes.name);
23+
return b.name.localeCompare(a.name);
2424
} else {
2525
return 0;
2626
}
@@ -32,4 +32,4 @@ export default class LibraryDetailsBooksIndexRoute extends Route {
3232
// else if(s === 'desc') return books.sortBy('name').reverse();
3333
// else return books;
3434
// let books = await fetch(`/libraries/${library_id}/books`).then((response)=>response.json()).then((r)=>r.data);
35-
// let books = this.modelFor('library.details').books.data.slice();
35+
// let books = this.modelFor('library.details').books.data.slice();

app/routes/library/details/read-book.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,22 @@ export default class LibraryDetailsReadBookRoute extends Route {
2222
async model({ readbook_id }) {
2323
let library_id = this.paramsFor('library.details').library_id;
2424
let url = `/libraries/${library_id}/books/${readbook_id}`;
25-
let book = this.data.getData(url);
25+
let data = await this.data.getData(url);
2626

27-
if (!book) {
27+
if (!data) {
2828
this.router.transitionTo('not-found', 'notfound');
2929
}
30-
return book;
30+
return data.book;
3131
}
3232
}
3333

34+
// let book;
35+
// let library = this.modelFor('library.details');
36+
// library.books.forEach(element => {
37+
// if (element.id == readbook_id) {
38+
// book = element;
39+
// return
40+
// }
41+
// });
3442

35-
// let book;
36-
// let library = this.modelFor('library.details');
37-
// library.books.forEach(element => {
38-
// if (element.id == readbook_id) {
39-
// book = element;
40-
// return
41-
// }
42-
// });
43-
44-
// let book = await fetch(`/books/${readbook_id}`).then((response)=>response.json()).then((r)=>r.data);
43+
// let book = await fetch(`/books/${readbook_id}`).then((response)=>response.json()).then((r)=>r.data);

app/routes/library/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default class LibraryIndexRoute extends Route {
44
model() {
55
let data = fetch('/libraries')
66
.then((response) => response.json())
7-
.then((r) => r.data);
7+
.then((r) => r.libraries);
88
return data;
99
}
1010
}

app/services/data.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@ import Service from '@ember/service';
22
import { tracked } from '@glimmer/tracking';
33

44
export default class DataService extends Service {
5-
@tracked booksReq = 0;
6-
@tracked libraryReq = 0;
5+
@tracked booksReq = 0;
6+
@tracked libraryReq = 0;
77

8-
async getData(url) {
9-
let data = await fetch(url)
10-
.then((response) => response.json())
11-
.then((r) => r.data);
12-
13-
if (data) {
14-
if (data.type == 'libraries') {
15-
this.libraryReq++;
16-
}
17-
else if (data.type == 'books') {
18-
this.booksReq++;
19-
}
20-
}
21-
return data;
8+
async getData(url) {
9+
let data = await fetch(url)
10+
.then((response) => {
11+
if (response.ok) return response.json();
12+
});
13+
14+
if (data) {
15+
if (data.library) {
16+
this.libraryReq++;
17+
} else if (data.book) {
18+
this.booksReq++;
19+
}
2220
}
21+
return data;
22+
}
2323
}
24+
// if (data.type == 'libraries') {
25+
// this.libraryReq++;
26+
// } else if (data.type == 'books') {
27+
// this.booksReq++;
28+
// }
2429

2530
// let book;
2631
// lib.books.forEach(element => {
@@ -38,7 +43,6 @@ export default class DataService extends Service {
3843
// }
3944
// });
4045

41-
4246
// async getData(url) {
4347
// let arr = url.split('/');
4448

@@ -70,4 +74,4 @@ export default class DataService extends Service {
7074
// if (lib) this.libraryReq++;
7175
// return lib;
7276
// }
73-
// }
77+
// }

app/templates/library/details.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div align="center">
22
<div class="box-view">
3-
<h2>{{this.model.attributes.name}}</h2>
4-
<p>Location : {{this.model.attributes.location}}</p>
5-
<p>Timing : {{this.model.attributes.timing}}</p>
3+
<h2>{{this.model.name}}</h2>
4+
<p>Location : {{this.model.location}}</p>
5+
<p>Timing : {{this.model.timing}}</p>
66
<LinkTo @route="library.details.books" @replace={{true}}>Books Available</LinkTo>
77
</div>
88
</div>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{{page-title "Book"}}
22

33
<div align="center">
4-
<h3>{{this.model.attributes.name}}</h3>
4+
<h3>{{this.model.name}}</h3>
55
<h4><u>DESCRIPTION</u></h4>
6-
<p>Author : {{this.model.attributes.author}}</p>
7-
<p>Publisher : {{this.model.attributes.publisher}}</p>
8-
<p>Date of Publication : {{get this.model.attributes "published-date"}}</p>
6+
<p>Author : {{this.model.author}}</p>
7+
<p>Publisher : {{this.model.publisher}}</p>
8+
<p>Date of Publication : {{this.model.publishedDate}}</p>
99
<LinkTo @route="library.details.read-book" @model={{this.model.id}}>Read book</LinkTo>
1010
</div>

app/templates/library/details/books/index.hbs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
<LinkTo @route="library.details.books" @query={{hash sort="desc" }}>Descending</LinkTo>
66
</div>
77
</div>
8-
{{log this.model}}
98
<div style="text-align: center;">
109
<ul class="myUL">
1110
{{#each this.model as |book|}}
1211
<div style="padding:15px">
1312
<li>
14-
<LinkTo @route="library.details.books.book" @model={{book.id}}>{{book.attributes.name}}</LinkTo>
13+
<LinkTo @route="library.details.books.book" @model={{book.id}}>{{book.name}}</LinkTo>
1514
</li>
1615
</div>
1716
{{/each}}

app/templates/library/details/read-book.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{page-title "ReadBook"}}
22

33
<div align="center">
4-
<h3>{{this.model.attributes.name}}</h3>
4+
<h3>{{this.model.name}}</h3>
55

66
<label for="name">Name</label>
77
<Input id="name" @value={{this.name}} @type="text" /><br><br>

app/templates/library/index.hbs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{{page-title "Index"}}
2-
{{log this.model}}
32
<div align="center">
43
<h1>Libraries</h1>
54
{{#each this.model as |lib|}}
65
<div style="padding:10px">
7-
<LinkTo @route="library.details" @model={{lib.id}}>{{lib.attributes.name}}</LinkTo>
6+
<LinkTo @route="library.details" @model={{lib.id}}>{{lib.name}}</LinkTo>
87
</div>
98
{{/each}}
109
</div>

mirage/config.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
discoverEmberDataModels,
33
// applyEmberDataSerializers,
44
} from 'ember-cli-mirage';
5-
import { createServer } from 'miragejs';
5+
import { Response, createServer } from 'miragejs';
66

77
export default function (config) {
88
let finalConfig = {
@@ -29,20 +29,24 @@ function routes() {
2929
let id = request.params.id;
3030
let bookIds = schema.libraries.find(id).attrs.bookIds;
3131
let books = schema.books.find(bookIds);
32-
33-
return this.serialize(books);
32+
return books;
3433
});
3534

3635
this.get('/libraries/:id/books/:book_id', function (schema, request) {
3736
let id = request.params.id;
3837
let book_id = request.params.book_id;
3938
let bookIds = schema.libraries.find(id).attrs.bookIds;
39+
4040
let book;
41-
bookIds.forEach(bookId => {
42-
if(bookId==book_id){
43-
book = schema.books.find(book_id);
41+
bookIds.forEach((bookId) => {
42+
if (bookId == book_id) {
43+
book = schema.books.find(book_id);
4444
}
4545
});
46+
47+
if (!book) {
48+
return new Response(500, {}, { error: 'Book not found' });
49+
}
4650
return book;
4751
});
4852

mirage/models/library.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

mirage/serializers/application.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { JSONAPISerializer } from 'miragejs';
1+
import { RestSerializer } from 'miragejs';
22

3-
export default JSONAPISerializer.extend({
4-
});
3+
export default RestSerializer.extend({});

mirage/serializers/book.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ApplicationSerializer from './application';
22

33
export default ApplicationSerializer.extend({
4-
include: ['library'],
4+
// include: ['library'],
55
});

mirage/serializers/library.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import ApplicationSerializer from './application';
22

33
export default ApplicationSerializer.extend({
4-
include: ['books'],
4+
// include: ['books'],
5+
6+
// serialize(library, request) {
7+
// // console.log(library);
8+
// let data = {
9+
// data: {
10+
// id: library.id,
11+
// location: library.location,
12+
// name: library.name,
13+
// timing: library.timing
14+
// }
15+
// }
16+
// return data;
17+
// }
518
});

0 commit comments

Comments
 (0)