File: /var/www/html/xfacil.desafio.com.py/app/Http/Livewire/Frontnoticias.php
<?php
namespace App\Http\Livewire;
use Livewire\WithPagination;
use App\Models\Noticia;
use App\Models\Emoticonnoticia;
use App\Models\Comentarionoticia;
use Auth;
use DB;
use Livewire\Component;
class Frontnoticias extends Component{
use WithPagination;
protected $queryString = ['search' => ['except' => '']];
protected $paginationTheme = 'bootstrap';
public $not_id,$titulo,$video,$comentario,$comentario_id,$estado = 'emocion',$megusta,$meencanta,$medivierte,$measombra,$meentristece,$meenoja,$emocion;
public $updateMode = false;
public $search='';
public function mount(){
if($primera = Noticia::where('titulo','LIKE','%'.$this->search.'%')->orderBy('id','desc')->first()){
$this->not_id = $primera->id;
$this->titulo = $primera->titulo;
$this->video = $primera->video;
if($emocion = Emoticonnoticia::where('noticia_id',$primera->id)->first()){
$this->estado = $emocion->emocion;
}
$this->megusta = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-gusta')->count();
$this->meencanta = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-encanta')->count();
$this->medivierte = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-divierta')->count();
$this->measombra = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-asombra')->count();
$this->meentristece = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-entristece')->count();
$this->meenoja = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-enoja')->count();
}
}
public function render(){
$noticias = Noticia::where('titulo','LIKE','%'.$this->search.'%')->orderBy('id','desc')->paginate(20);
$emociones = Emoticonnoticia::where('usuario_id',Auth::user()->id)->get();
$comentarios = DB::table('comentarionoticias as cn')
->join('users as u','cn.usuario_id','u.id')
->select('cn.*','u.name')
->where('noticia_id',$this->not_id)->get();
return view('livewire.frontnoticias',["noticias"=>$noticias,"emociones"=>$emociones,"comentarios"=>$comentarios]);
}
private function resetInputFields(){
$this->comentario = '';
$this->comentario_id = '';
}
public function addemocion($noticia_id,$emocion,$estado){
if($emociondelete = Emoticonnoticia::where('usuario_id', Auth::user()->id)->where('noticia_id',$noticia_id)->first()){
$emociondelete->delete();
}
if($estado != $emocion){
Emoticonnoticia::create([
'usuario_id' => Auth::user()->id,
'noticia_id' => $noticia_id,
'emocion' => $emocion,
]);
}
$this->estado = $emocion;
$this->megusta = Emoticonnoticia::where('noticia_id',$noticia_id)->where('emocion','me-gusta')->count();
$this->meencanta = Emoticonnoticia::where('noticia_id',$noticia_id)->where('emocion','me-encanta')->count();
$this->medivierte = Emoticonnoticia::where('noticia_id',$noticia_id)->where('emocion','me-divierta')->count();
$this->measombra = Emoticonnoticia::where('noticia_id',$noticia_id)->where('emocion','me-asombra')->count();
$this->meentristece = Emoticonnoticia::where('noticia_id',$noticia_id)->where('emocion','me-entristece')->count();
$this->meenoja = Emoticonnoticia::where('noticia_id',$noticia_id)->where('emocion','me-enoja')->count();
}
public function cambiar($id){
$primera = Noticia::find($id);
$this->not_id = $primera->id;
$this->titulo = $primera->titulo;
$this->video = $primera->video;
if($emocion = Emoticonnoticia::where('noticia_id',$primera->id)->first()){
$this->estado = $emocion->emocion;
}else{
$this->estado = 'emocion';
}
$this->megusta = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-gusta')->count();
$this->meencanta = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-encanta')->count();
$this->medivierte = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-divierta')->count();
$this->measombra = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-asombra')->count();
$this->meentristece = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-entristece')->count();
$this->meenoja = Emoticonnoticia::where('noticia_id',$primera->id)->where('emocion','me-enoja')->count();
}
public function guardarcomentario(){
$validatedDate = $this->validate([
'comentario' => 'required',
],
[
'comentario.required' => 'El campo comentario es requerido',
]);
Comentarionoticia::create([
'usuario_id' => Auth::user()->id,
'noticia_id' => $this->not_id,
'comentario' => $this->comentario,
]);
$this->updateMode = false;
$this->resetInputFields();
$this->emit('alert', ['type' => 'success', 'message' => 'Comentario agregado correctamente.']);
}
public function editcomentario($id){
$comentario = Comentarionoticia::find($id);
$this->comentario_id = $id;
$this->comentario = $comentario->comentario;
$this->updateMode = true;
}
public function updatecomentario(){
$validatedDate = $this->validate([
'comentario' => 'required',
],
[
'comentario.required' => 'El campo comentario es requerido',
]);
$comentario = Comentarionoticia::find($this->comentario_id);
$comentario->update([
'usuario_id' => Auth::user()->id,
'comentario' => $this->comentario,
]);
$this->updateMode = false;
$this->resetInputFields();
$this->emit('alert', ['type' => 'success', 'message' => 'Comentario editado correctamente.']);
}
public function deletecomentario($id){
$comentario = Comentarionoticia::find($id);
$comentario->delete();
$this->emit('alert', ['type' => 'error', 'message' => 'Comentario eliminado correctamente.']);
}
}