-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Location For Library Books #3709
base: master
Are you sure you want to change the base?
Changes from all commits
41815b9
30f061c
5fba081
1ccf77d
d7d5d7d
dfdd162
33db6cf
9d4f77e
261bb11
ab7ea3e
946411a
b671887
b4cb784
c97e18a
35673d8
3f2ec3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ | |
namespace App\Http\Controllers\KnowledgeCafe\Library; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\DB; | ||
use App\Http\Requests\KnowledgeCafe\Library\BookRequest; | ||
use App\Models\KnowledgeCafe\Library\Book; | ||
use App\Models\KnowledgeCafe\Library\BookAMonth; | ||
use App\Models\KnowledgeCafe\Library\BookCategory; | ||
use App\Models\KnowledgeCafe\Library\BookLocation; | ||
use Modules\Operations\Entities\OfficeLocation; | ||
use App\Services\BookServices; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
|
@@ -29,7 +32,7 @@ public function index(Request $request) | |
$searchCategory = $request->category_name ?? false; | ||
$searchString = request()->has('search') ? request()->input('search') : false; | ||
$categories = BookCategory::orderBy('name')->get(); | ||
|
||
$bookLocations = BookLocation::withTrashed()->with('location')->get(); | ||
switch (request()) { | ||
case request()->has('wishlist'): | ||
$books = auth()->user()->booksInWishlist; | ||
|
@@ -48,9 +51,18 @@ public function index(Request $request) | |
$books->load('borrowers'); | ||
$wishlistedBooksCount = auth()->user()->booksInWishlist->count(); | ||
$booksBorrowedCount = auth()->user()->booksBorrower->count(); | ||
|
||
return view('knowledgecafe.library.books.index', compact('books', 'loggedInUser', 'categories', 'wishlistedBooksCount', 'booksBorrowedCount')); | ||
} | ||
$officeLocations = $this->officeLocationData(); | ||
|
||
$books = $books->map(function ($book) use ($bookLocations) { | ||
$locations = $bookLocations->where('book_id', $book->id); | ||
$copies = []; | ||
foreach ($locations as $location) { | ||
$copies[$location->office_location_id] = $location->number_of_copies; | ||
} | ||
$book->copies = $copies; | ||
return $book; | ||
}); | ||
Comment on lines
+56
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Move data manipulation logic to a model or service class Processing data within the controller can make the code less readable and harder to maintain. Consider moving the logic that maps books and attaches copies to a method within the |
||
return view('knowledgecafe.library.books.index', compact('books', 'loggedInUser', 'categories', 'wishlistedBooksCount', 'booksBorrowedCount', 'bookLocations', 'officeLocations')); } | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
|
@@ -59,9 +71,12 @@ public function index(Request $request) | |
*/ | ||
public function create() | ||
{ | ||
return view('knowledgecafe.library.books.create'); | ||
return view('knowledgecafe.library.books.create', [ | ||
'officeLocations' => $this->officeLocationData(), | ||
]); | ||
} | ||
|
||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
|
@@ -71,9 +86,13 @@ public function store(BookRequest $request) | |
{ | ||
$validatedData = $request->validated(); | ||
$ISBN = $validatedData['isbn'] ?? null; | ||
Book::firstOrCreate(['isbn' => $ISBN], $validatedData); | ||
|
||
return response()->json(['error' => false]); | ||
$copiesLocation = $validatedData['copies']; | ||
$book = Book::firstOrCreate(['isbn' => $ISBN], $validatedData); | ||
$this->storeBookLocationWise($book->id, $copiesLocation); | ||
return response()->json([ | ||
'error' => false, | ||
'book_id' => $book->id | ||
]); | ||
Comment on lines
+89
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate Using |
||
} | ||
|
||
/** | ||
|
@@ -106,10 +125,10 @@ public function show(Book $book) | |
public function update(BookRequest $request, Book $book) | ||
{ | ||
$validatedData = $request->validated(); | ||
if (isset($validatedData['number_of_copies'])) { | ||
$book->number_of_copies = $validatedData['number_of_copies']; | ||
if (isset($validatedData['copies'])) { | ||
$copiesLocation = $request['copies']; | ||
$book->save(); | ||
|
||
$this->updatedBookLocationWise($book->id, $copiesLocation); | ||
return response()->json(['isUpdated' => $book]); | ||
} | ||
if (isset($validatedData['categories'])) { | ||
|
@@ -342,4 +361,37 @@ public function bookAMonthIndex() | |
'booksBorrowedCount' => $booksBorrowedCount, | ||
]); | ||
} | ||
|
||
public function storeBookLocationWise($bookId, $copiesLocation) | ||
{ | ||
$locationCopiesData = $copiesLocation; | ||
foreach ($locationCopiesData as $locationId => $copies) { | ||
BookLocation::create([ | ||
'book_id' => $bookId, | ||
'office_location_id' => $locationId, | ||
'number_of_copies' => $copies | ||
]); | ||
} | ||
} | ||
Comment on lines
+365
to
+375
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent duplicate The |
||
|
||
public function officeLocationData() | ||
{ | ||
$officeLocations = OfficeLocation::all(); | ||
$officeLocationsData = $officeLocations->map(function ($officeLocation) { | ||
return [ | ||
'center_id' => $officeLocation->id, | ||
'centre_name' => $officeLocation->centre_name, | ||
]; | ||
}); | ||
return $officeLocationsData; | ||
} | ||
|
||
public function updatedBookLocationWise($bookId, $copiesLocation) { | ||
foreach ($copiesLocation as $locationId => $copies) { | ||
DB::table('library_book_location') | ||
->where('book_id', $bookId) | ||
->where('office_location_id', $locationId) | ||
->update(['number_of_copies' => $copies]); | ||
} | ||
} | ||
Comment on lines
+389
to
+396
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle non-existent records in The |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Modules\User\Entities\User; | ||
use App\Models\KnowledgeCafe\Library\BookLocation; | ||
|
||
class Book extends Model | ||
{ | ||
|
@@ -174,4 +175,9 @@ public function bookAMonths() | |
{ | ||
return $this->hasMany(BookAMonth::class, 'library_book_id'); | ||
} | ||
|
||
public function bookLocations() | ||
{ | ||
return $this->hasMany(BookLocation::class, 'book_id'); | ||
} | ||
Comment on lines
+179
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Incorrect relationship definition in BookLocation model The relationship is incorrectly defined in the BookLocation model. It's using
public function book()
{
- return $this->hasOne(Book::class, 'id', 'book_id');
+ return $this->belongsTo(Book::class, 'book_id');
} 🔗 Analysis chainAdd PHPDoc for the relationship method Please add PHPDoc to document the relationship and return type. + /**
+ * Get the locations associated with the book.
+ *
+ * @return \Illuminate\Database\Eloquent\Relations\HasMany
+ */
public function bookLocations()
{
return $this->hasMany(BookLocation::class, 'book_id');
} Verify the inverse relationship The relationship looks correct, but let's verify the inverse relationship in BookLocation model. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check if BookLocation model has the inverse belongsTo relationship
ast-grep --pattern 'class BookLocation extends Model {
$$$
public function book() {
$$$
belongsTo($$$Book::class$$$)
$$$
}
$$$
}'
Length of output: 151 Script: #!/bin/bash
# Let's try a different approach to find the BookLocation model and check its relationships
# First, find the BookLocation model file
fd "BookLocation.php" --type f
# Then let's examine its content
rg "class BookLocation" -A 20 -B 5
Length of output: 1855 |
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||||||||||||||
<?php | ||||||||||||||||||
|
||||||||||||||||||
namespace App\Models\KnowledgeCafe\Library; | ||||||||||||||||||
|
||||||||||||||||||
use Illuminate\Database\Eloquent\Model; | ||||||||||||||||||
use Illuminate\Database\Eloquent\SoftDeletes; | ||||||||||||||||||
use Modules\Operations\Entities\OfficeLocation; | ||||||||||||||||||
|
||||||||||||||||||
class BookLocation extends Model | ||||||||||||||||||
{ | ||||||||||||||||||
use SoftDeletes; | ||||||||||||||||||
|
||||||||||||||||||
protected $table = 'library_book_location'; | ||||||||||||||||||
protected $fillable = ['book_id', 'office_location_id', 'number_of_copies']; | ||||||||||||||||||
protected $dates = ['deleted_at']; | ||||||||||||||||||
|
||||||||||||||||||
public function book() | ||||||||||||||||||
{ | ||||||||||||||||||
return $this->hasOne(Book::class, 'id', 'book_id'); | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+17
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect relationship type used. The Apply this fix: public function book()
{
- return $this->hasOne(Book::class, 'id', 'book_id');
+ return $this->belongsTo(Book::class);
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
public function location() | ||||||||||||||||||
{ | ||||||||||||||||||
return $this->hasOne(OfficeLocation::class, 'id', 'office_location_id'); | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect relationship type used. Similar to the book relationship, the location relationship should use Apply this fix: public function location()
{
- return $this->hasOne(OfficeLocation::class, 'id', 'office_location_id');
+ return $this->belongsTo(OfficeLocation::class, 'office_location_id');
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||||||||||||||||||||||||||||
<?php | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
use Illuminate\Database\Migrations\Migration; | ||||||||||||||||||||||||||||||||
use Illuminate\Database\Schema\Blueprint; | ||||||||||||||||||||||||||||||||
use Illuminate\Support\Facades\Schema; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
class CreateLibraryBookLocationCopiesTable extends Migration | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||
* Run the migrations. | ||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||
* @return void | ||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||
public function up() | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Schema::create('library_book_location', function (Blueprint $table) { | ||||||||||||||||||||||||||||||||
$table->id(); | ||||||||||||||||||||||||||||||||
$table->unsignedInteger('book_id'); | ||||||||||||||||||||||||||||||||
$table->unsignedBigInteger('office_location_id'); | ||||||||||||||||||||||||||||||||
$table->integer('number_of_copies'); | ||||||||||||||||||||||||||||||||
$table->timestamps(); | ||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||
Comment on lines
+16
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add unique constraint and validation for number_of_copies.
Consider this improved schema: Schema::create('library_book_location', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('book_id');
$table->unsignedBigInteger('office_location_id');
- $table->integer('number_of_copies');
+ $table->unsignedInteger('number_of_copies');
$table->timestamps();
+ $table->unique(['book_id', 'office_location_id']);
}); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
Schema::table('library_book_location', function (Blueprint $table) { | ||||||||||||||||||||||||||||||||
$table->foreign('book_id')->references('id')->on('library_books'); | ||||||||||||||||||||||||||||||||
$table->foreign('office_location_id')->references('id')->on('office_locations'); | ||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||
* Reverse the migrations. | ||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||
* @return void | ||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||
public function down() | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Schema::dropIfExists('library_book_location'); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor to use Eloquent models instead of the DB facade
Using the
DB
facade directly can bypass Eloquent's ORM benefits such as relationships, events, and attribute casting. Consider using Eloquent models for database operations to maintain consistency and improve code readability.