Created
May 15, 2021 20:26
-
-
Save Chriz76/049a1d98c5385b1aee6ddc0b64540166 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
using Newtonsoft.Json; | |
using RabbitMQ.Client; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Threading.Tasks; | |
using UserService.Data; | |
using UserService.Entities; | |
namespace UserService.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class UsersController : ControllerBase | |
{ | |
private readonly UserServiceContext _context; | |
public UsersController(UserServiceContext context) | |
{ | |
_context = context; | |
} | |
[HttpGet] | |
public async Task<ActionResult<IEnumerable<User>>> GetUser() | |
{ | |
return await _context.User.ToListAsync(); | |
} | |
private void PublishToMessageQueue(string integrationEvent, string eventData) | |
{ | |
// TOOO: Reuse and close connections and channel, etc, | |
var factory = new ConnectionFactory(); | |
var connection = factory.CreateConnection(); | |
var channel = connection.CreateModel(); | |
var body = Encoding.UTF8.GetBytes(eventData); | |
channel.BasicPublish(exchange: "user", | |
routingKey: integrationEvent, | |
basicProperties: null, | |
body: body); | |
} | |
[HttpPut("{id}")] | |
public async Task<IActionResult> PutUser(int id, User user) | |
{ | |
_context.Entry(user).State = EntityState.Modified; | |
await _context.SaveChangesAsync(); | |
var integrationEventData = JsonConvert.SerializeObject(new | |
{ | |
id = user.ID, | |
newname = user.Name | |
}); | |
PublishToMessageQueue("user.update", integrationEventData); | |
return NoContent(); | |
} | |
[HttpPost] | |
public async Task<ActionResult<User>> PostUser(User user) | |
{ | |
_context.User.Add(user); | |
await _context.SaveChangesAsync(); | |
var integrationEventData = JsonConvert.SerializeObject(new | |
{ | |
id = user.ID, | |
name = user.Name | |
}); | |
PublishToMessageQueue("user.add", integrationEventData); | |
return CreatedAtAction("GetUser", new { id = user.ID }, user); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment