public void Post(Curtidas curtida) { try { _curtidaRepository.Adicionar(curtida); } catch (Exception ex) { throw new Exception(ex.Message); } }
public void Adicionar(Curtidas curtida) { try { _ctx.Curtidas.Add(curtida); _ctx.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <string> Curtir(int postagemId) { var postagens = await _postagemRepository .ObterPostagens() .ConfigureAwait(false); if (!postagens.Contains(postagemId)) { throw new ArgumentException("Não existe publicação com esse ID"); } var usuarioId = _logado.ObterUsuarioLogado(); var curtida = await _curtidasRepository .GetByUsuarioIdAndPostagemIdAsync(usuarioId, postagemId) .ConfigureAwait(false); var usuarioPostagemId = await _postagemRepository .GetUsuarioIdByPostagemId(postagemId); var amigosId = await _amigosRepository .ObterListaDeAmigos(usuarioId) .ConfigureAwait(false); if (amigosId.Contains(usuarioPostagemId) || usuarioPostagemId == usuarioId) { if (curtida != null) { var descurtiu = await _curtidasRepository .Descurtir(curtida.Id) .ConfigureAwait(false); var resultado = ("Você descurtiu a postagem de número " + postagemId); return(resultado); } else { var novaCurtida = new Curtidas(usuarioId, postagemId); var curtiu = await _curtidasRepository .Curtir(novaCurtida) .ConfigureAwait(false); var resultado = ("Você curtiu a postagem de número " + postagemId); return(resultado); } } else { throw new Exception("Você não pode curtir essa publicação, porque você não segue este usuário"); } }
public Curtidas BuscarPorId(Guid id) { try { Curtidas curtida = _ctx.Curtidas.Find(id); return(curtida); } catch (Exception ex) { throw new Exception(ex.Message); } }
public void Remover(Guid id) { try { Curtidas curtida = BuscarPorId(id); if (curtida == null) { throw new Exception("Curtida não encontrado"); } _ctx.Curtidas.Remove(curtida); _ctx.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <string> Curtir(Curtidas curtida) { using (var con = new SqlConnection(_configuration["ConnectionString"])) { var sqlCmd = @"INSERT INTO Curtidas (PostagemId, UsuarioId) VALUES (@postagemId, @usuarioId); SELECT scope_identity();"; using (var cmd = new SqlCommand(sqlCmd, con)) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("postagemId", curtida.PostagemId); cmd.Parameters.AddWithValue("usuarioId", curtida.UsuarioId); con.Open(); var id = await cmd.ExecuteScalarAsync().ConfigureAwait(false); return("Você curtiu a postagem número " + curtida.PostagemId); } } }