public async Task <IActionResult> GetPublicationById(int id) { try { var resp = await _publicationServices.GetPublicationById(id); if (resp == null) { return(NotFound(new { message = $"Publicacion con Id = {id} no encontrado" })); } return(Ok(resp)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, new { message = e.Message })); } }
public async Task <bool> AddSale(SaleOrderDTO saleOrderDTO) { using (var db = new SocialNetworkDeveloperContext()) { using (var transaction = db.Database.BeginTransaction()) { try { var seller = await _userServices.GetUserByID((int)saleOrderDTO.IdUsuario); var buyer = await _userServices.GetUserByID((int)saleOrderDTO.IdComprador); var publication = await _publicationServices.GetPublicationById((int)saleOrderDTO.IdPublicacion); var saleOrder = _mapper.Map <OrdenesVenta>(saleOrderDTO); saleOrder.FechaHoraOrdenVenta = DateTime.Now; //saleOrder.TotalVentaConIva = saleOrderDTO.Cantidad * (publication.Precio + (publication.Precio * 0.13m)); saleOrder.TotalVenta = saleOrderDTO.Cantidad * publication.Precio; saleOrder.EstadoOrdenVenta = 1; //pendiente db.OrdenesVentas.Add(saleOrder); await db.SaveChangesAsync(); //Creacion de orden de compra OrdenesCompra objCompra = new OrdenesCompra() { IdOrdenVenta = saleOrder.IdOrdenVenta, IdPublicacion = publication.IdPublicacion, IdUsuario = buyer.IdUsuario, FechaHoraOrdenCompra = DateTime.Now, //TotalCompraConIva = saleOrderDTO.Cantidad * (publication.Precio + (publication.Precio * 0.13m)), TotalCompra = saleOrderDTO.Cantidad * publication.Precio, EstadoOrdenCompra = 1, //pendiente Cantidad = saleOrderDTO.Cantidad }; db.OrdenesCompras.Add(objCompra); await db.SaveChangesAsync(); EmailDTO emailDTO = new EmailDTO() { EmailBuyer = buyer.CorreoElectronico, EmailSeller = seller.CorreoElectronico, Title = string.Format("Se Realizo una solicitud de compra/venta de la Publicaicon {0}", publication.Titulo), MessagesSeller = string.Format(@"Estimado/a <b>{0}</b> se ha realizado una solicitud de venta a travez de nuestra aplicación, <br/> Detalle De la Venta <br/> Publicación: <b>{1}</b> <br/> Cantidad: <b>{2}</b> <br/> Cliente: <b>{3}</b> <br/> Telefono: <b>{4}</b> <br/> Dirección Entrega: <b>{5}</b> <br/> Comentario: <b>{6}</b>" , seller.NombreCompleto, publication.Titulo, saleOrderDTO.Cantidad, buyer.NombreCompleto, buyer.TelefonoContacto, saleOrderDTO.DireccionEntrega, saleOrderDTO.Comentario), MessagesBuyer = string.Format(@"Estimado/a <b>{0}</b> se ha realizado una solicitud de compra a travez de nuestra aplicación, <br/> Detalle De la Compra <br/> Publicación: <b>{1}</b> <br/> Cantidad: <b>{2}</b>" , buyer.NombreCompleto, publication.Titulo, saleOrderDTO.Cantidad), }; //si falla envio correo no registrar la venta var email = await _emailServices.SendEmail(emailDTO); if (!email) { throw new Exception(); } //envio sms twilio var smsSeller = await _twilioServices.SendSMS(seller.TelefonoContacto, "Se Realizo una solicitud de compra/venta en SNB&S"); if (!smsSeller) { throw new Exception(); } var smsBuyer = await _twilioServices.SendSMS(buyer.TelefonoContacto, "Acabas de realizar una solicitud de compra en SNB&S"); if (!smsBuyer) { throw new Exception(); } await transaction.CommitAsync(); return(true); } catch (Exception) { transaction.Rollback(); return(false); } } } }