|
@@ -35,22 +35,6 @@ class MusicRepository
|
|
|
|
|
|
return $this->postMusicCategory->where($where)->orderBy('id', 'asc')->paginate($perPage);
|
|
return $this->postMusicCategory->where($where)->orderBy('id', 'asc')->paginate($perPage);
|
|
}
|
|
}
|
|
-//
|
|
|
|
-// public function postMusicList($request)
|
|
|
|
-// {
|
|
|
|
-// $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
|
|
-//
|
|
|
|
-// $postMusicList = $this->postMusicCategoryRel
|
|
|
|
-// ->join('post_music_category', 'post_music_category.id', '=', 'post_music_category_rel.music_category_id')
|
|
|
|
-// ->join('post_music', 'post_music.id', '=', 'post_music_category_rel.mid')
|
|
|
|
-// ->select('post_music.*')
|
|
|
|
-// ->where('post_music_category.id', '=', $request['category_id'])
|
|
|
|
-// ->orderBy('id', 'desc')
|
|
|
|
-// ->paginate($perPage);
|
|
|
|
-//
|
|
|
|
-// return $postMusicList;
|
|
|
|
-//
|
|
|
|
-// }
|
|
|
|
|
|
|
|
public function categoryCreate($request)
|
|
public function categoryCreate($request)
|
|
{
|
|
{
|
|
@@ -113,11 +97,22 @@ class MusicRepository
|
|
public function musicList($request)
|
|
public function musicList($request)
|
|
{
|
|
{
|
|
$perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
$perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
|
|
+ $where = [];
|
|
|
|
+ if(isset($request['id'])){
|
|
|
|
+ $where[] = ['post_music_category_rel.id', '=', $request['id']];
|
|
|
|
+ }
|
|
|
|
+ if(isset($request['name'])){
|
|
|
|
+ $where[] = ['post_music.name', 'like', "%{$request['name']}%"];
|
|
|
|
+ }
|
|
|
|
+ if(isset($request['category_id'])){
|
|
|
|
+ $where[] = ['post_music_category.id', '=', $request['category_id']];
|
|
|
|
+ }
|
|
|
|
|
|
$postMusicList = $this->postMusicCategoryRel
|
|
$postMusicList = $this->postMusicCategoryRel
|
|
->join('post_music_category', 'post_music_category.id', '=', 'post_music_category_rel.music_category_id')
|
|
->join('post_music_category', 'post_music_category.id', '=', 'post_music_category_rel.music_category_id')
|
|
->join('post_music', 'post_music.id', '=', 'post_music_category_rel.mid')
|
|
->join('post_music', 'post_music.id', '=', 'post_music_category_rel.mid')
|
|
- ->select('post_music.*','post_music_category.name as category_name')
|
|
|
|
|
|
+ ->select('post_music_category_rel.id','post_music.name','post_music.url','post_music.music_duration','post_music.created_at','post_music_category.name as category_name')
|
|
|
|
+ ->where($where)
|
|
->orderBy('id', 'desc')
|
|
->orderBy('id', 'desc')
|
|
->paginate($perPage);
|
|
->paginate($perPage);
|
|
|
|
|
|
@@ -131,66 +126,64 @@ class MusicRepository
|
|
$data = [
|
|
$data = [
|
|
'name' => $request['name'],
|
|
'name' => $request['name'],
|
|
'url' => $url,
|
|
'url' => $url,
|
|
|
|
+ 'music_duration' => 60,
|
|
];
|
|
];
|
|
$date = date('Y-m-d H:i:s');
|
|
$date = date('Y-m-d H:i:s');
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
- try{
|
|
|
|
- $create_music = $this->postMusic->create($data);
|
|
|
|
- if ($create_music){
|
|
|
|
- if ($this->postMusicCategoryRel->where(['mid' => $request['mid'], 'music_category_id' => $request['category_id']])->exists()) {
|
|
|
|
- throw new HttpException(500, '该音乐已经存在');
|
|
|
|
- }
|
|
|
|
|
|
+ try {
|
|
|
|
+ $res = $this->postMusic->create($data);
|
|
|
|
+ if ($res) {
|
|
|
|
+ $mid = $res->id;
|
|
$create_category_rel = [
|
|
$create_category_rel = [
|
|
- 'mid' => $create_music['id'],
|
|
|
|
|
|
+ 'mid' => $mid,
|
|
'music_category_id' => $request['category_id'],
|
|
'music_category_id' => $request['category_id'],
|
|
'created_at' => $date,
|
|
'created_at' => $date,
|
|
- 'updated_at' => $date,
|
|
|
|
|
|
+ 'updated_at' => $date
|
|
];
|
|
];
|
|
$result = $this->postMusicCategoryRel->insert($create_category_rel);
|
|
$result = $this->postMusicCategoryRel->insert($create_category_rel);
|
|
- if (!$result){
|
|
|
|
|
|
+ if (!$result) {
|
|
throw new HttpException(500, '音乐与分类关联-添加失败');
|
|
throw new HttpException(500, '音乐与分类关联-添加失败');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
DB::commit();
|
|
DB::commit();
|
|
return Response::create();
|
|
return Response::create();
|
|
|
|
|
|
- }catch (QueryException $exception){
|
|
|
|
|
|
+ } catch (QueryException $exception) {
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '添加失败,请重试',
|
|
|
|
|
|
+ 'message' => '添加失败,请重试',
|
|
'error' => $exception->getMessage(),
|
|
'error' => $exception->getMessage(),
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public function musicEdit($request)
|
|
public function musicEdit($request)
|
|
{
|
|
{
|
|
- $post_music = $this->postMusic->where('id', $request['id'])->first();
|
|
|
|
|
|
+ $post_music_rel = $this->postMusicCategoryRel->select('mid')->where('id', $request['id'])->first();
|
|
|
|
+ $post_music = $this->postMusic->where('id',$post_music_rel->mid)->first();
|
|
$date = date('Y-m-d H:i:s');
|
|
$date = date('Y-m-d H:i:s');
|
|
$post_music->name = $request['name'];
|
|
$post_music->name = $request['name'];
|
|
$post_music->url = $request['url'];
|
|
$post_music->url = $request['url'];
|
|
|
|
+ $post_music->music_duration = 60;
|
|
$post_music->updated_at = $date;
|
|
$post_music->updated_at = $date;
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
try{
|
|
try{
|
|
- $res = $post_music->save();
|
|
|
|
- if ($res){
|
|
|
|
- if ($this->postMusicCategoryRel->where(['mid' => $request['mid'], 'music_category_id' => $request['category_id']])->exists()) {
|
|
|
|
- throw new HttpException(500, '该音乐已经存在');
|
|
|
|
- }
|
|
|
|
- $create_category_rel = [
|
|
|
|
- 'mid' => $request['id'],
|
|
|
|
- 'music_category_id' => $request['category_id'],
|
|
|
|
- 'created_at' => $date,
|
|
|
|
- 'updated_at' => $date,
|
|
|
|
- ];
|
|
|
|
- $result = $this->postMusicCategoryRel->where('mid',$post_music->id)->update($create_category_rel);
|
|
|
|
- if (!$result){
|
|
|
|
- throw new HttpException(500, '音乐与分类关联-修改失败');
|
|
|
|
|
|
+ $res = $post_music->save();
|
|
|
|
+ if ($res) {
|
|
|
|
+ $create_category_rel = [
|
|
|
|
+ 'mid' => $post_music->id,
|
|
|
|
+ 'music_category_id' => $request['category_id'],
|
|
|
|
+ 'created_at' => $date,
|
|
|
|
+ 'updated_at' => $date,
|
|
|
|
+ ];
|
|
|
|
+ $result = $this->postMusicCategoryRel->where('id',$request['id'])->update($create_category_rel);
|
|
|
|
+ if (!$result){
|
|
|
|
+ throw new HttpException(500, '音乐与分类关联-修改失败');
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+
|
|
DB::commit();
|
|
DB::commit();
|
|
return Response::create();
|
|
return Response::create();
|
|
|
|
|
|
@@ -207,15 +200,14 @@ class MusicRepository
|
|
|
|
|
|
public function musicDelete($request)
|
|
public function musicDelete($request)
|
|
{
|
|
{
|
|
- $post_music = $this->postMusic->where('id', $request['id'])->first();
|
|
|
|
|
|
+ $post_music_rel = $this->postMusicCategoryRel->where('id', $request['id'])->first();
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
try {
|
|
try {
|
|
- $res = $post_music->delete();
|
|
|
|
|
|
+ $res = $post_music_rel->delete();
|
|
if ($res) {
|
|
if ($res) {
|
|
- $post_category_rel = $this->postMusicCategoryRel->where('mid',$post_music->id)->delete();
|
|
|
|
-
|
|
|
|
- if (!$post_category_rel){
|
|
|
|
- throw new HttpException(500, '音乐与分类关联-删除失败');
|
|
|
|
|
|
+ $post_music = $this->postMusic->where('id',$post_music_rel->mid)->delete();
|
|
|
|
+ if (!$post_music){
|
|
|
|
+ throw new HttpException(500, '音乐删除失败');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
DB::commit();
|
|
DB::commit();
|