Update CollectionController

This commit is contained in:
Daniel Supernault 2022-10-02 18:55:16 -06:00
parent 84e152896b
commit b8ad594a05
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 27 additions and 27 deletions

View file

@ -77,6 +77,7 @@ class CollectionController extends Controller
$collection->visibility = $request->input('visibility'); $collection->visibility = $request->input('visibility');
$collection->save(); $collection->save();
CollectionService::deleteCollection($id);
return CollectionService::setCollection($collection->id, $collection); return CollectionService::setCollection($collection->id, $collection);
} }
@ -93,9 +94,9 @@ class CollectionController extends Controller
if($collection->items()->count() == 0) { if($collection->items()->count() == 0) {
abort(404); abort(404);
} }
$collection->title = e($request->input('title')); $collection->title = strip_tags($request->input('title'));
$collection->description = strip_tags($request->input('description')); $collection->description = strip_tags($request->input('description'));
$collection->visibility = strip_tags($request->input('visibility')); $collection->visibility = $request->input('visibility');
$collection->published_at = now(); $collection->published_at = now();
$collection->save(); $collection->save();
return CollectionService::setCollection($collection->id, $collection); return CollectionService::setCollection($collection->id, $collection);
@ -134,6 +135,7 @@ class CollectionController extends Controller
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId); $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
$count = $collection->items()->count(); $count = $collection->items()->count();
CollectionService::deleteCollection($collection->id);
if($count) { if($count) {
CollectionItem::whereCollectionId($collection->id) CollectionItem::whereCollectionId($collection->id)
@ -301,11 +303,6 @@ class CollectionController extends Controller
->whereIn('type', ['photo', 'photo:album', 'video']) ->whereIn('type', ['photo', 'photo:album', 'video'])
->findOrFail($postId); ->findOrFail($postId);
CollectionService::removeItem(
$collection->id,
$status->id
);
$item = CollectionItem::whereCollectionId($collection->id) $item = CollectionItem::whereCollectionId($collection->id)
->whereObjectType('App\Status') ->whereObjectType('App\Status')
->whereObjectId($status->id) ->whereObjectId($status->id)
@ -313,9 +310,17 @@ class CollectionController extends Controller
$item->delete(); $item->delete();
CollectionItem::whereCollectionId($collection->id)
->orderBy('created_at')
->get()
->each(function($item, $index) {
$item->order = $index;
$item->save();
});
$collection->updated_at = now(); $collection->updated_at = now();
$collection->save(); $collection->save();
CollectionService::setCollection($collection->id, $collection); CollectionService::deleteCollection($collection->id);
return 200; return 200;
} }

View file

@ -22,37 +22,32 @@ class CollectionService
public static function addItem($id, $sid, $score) public static function addItem($id, $sid, $score)
{ {
Redis::zadd(self::CACHE_KEY . 'items:' . $id, $score, $sid); return Redis::zadd(self::CACHE_KEY . 'items:' . $id, $score, $sid);
} }
public static function removeItem($id, $sid) public static function removeItem($id, $sid)
{ {
Redis::zrem(self::CACHE_KEY . 'items:' . $id, $sid); return Redis::zrem(self::CACHE_KEY . 'items:' . $id, $sid);
} }
public static function clearItems($id) public static function clearItems($id)
{ {
Redis::del(self::CACHE_KEY . 'items:' . $id); return Redis::del(self::CACHE_KEY . 'items:' . $id);
} }
public static function coldBootItems($id) public static function coldBootItems($id)
{ {
return Cache::remember(self::CACHE_KEY . 'items:' . $id, 86400, function() use($id) {
return CollectionItem::whereCollectionId($id) return CollectionItem::whereCollectionId($id)
->orderBy('order') ->orderBy('order')
->get() ->get()
->filter(function($item) use($id) {
return StatusService::get($item->object_id) != null;
})
->each(function($item) use ($id) { ->each(function($item) use ($id) {
self::addItem($id, $item->object_id, $item->order); return self::addItem($id, $item->object_id, $item->order);
}) })
->map(function($item) { ->map(function($item) {
return (string) $item->object_id; return (string) $item->object_id;
}) })
->values() ->values()
->toArray(); ->toArray();
});
} }
public static function count($id) public static function count($id)