-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathPlatformsController.cs
More file actions
91 lines (79 loc) · 2.97 KB
/
PlatformsController.cs
File metadata and controls
91 lines (79 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using PlatformService.AsyncDataServices;
using PlatformService.Data;
using PlatformService.Dtos;
using PlatformService.Models;
using PlatformService.SyncDataServices.Http;
namespace PlatformService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PlatformsController : ControllerBase
{
private readonly IPlatformRepo _repository;
private readonly IMapper _mapper;
private readonly ICommandDataClient _commandDataClient;
private readonly IMessageBusClient _messageBusClient;
public PlatformsController(
IPlatformRepo repository,
IMapper mapper,
ICommandDataClient commandDataClient,
IMessageBusClient messageBusClient)
{
_repository = repository;
_mapper = mapper;
_commandDataClient = commandDataClient;
_messageBusClient = messageBusClient;
}
[HttpGet]
public ActionResult<IEnumerable<PlatformReadDto>> GetPlatforms()
{
Console.WriteLine("--> Getting Platforms....");
var platformItems = _repository.GetAllPlatforms();
return Ok(_mapper.Map<IEnumerable<PlatformReadDto>>(platformItems));
}
[HttpGet("{id}", Name = "GetPlatformById")]
public ActionResult<PlatformReadDto> GetPlatformById(int id)
{
var platformItem = _repository.GetPlatformById(id);
if (platformItem != null)
{
return Ok(_mapper.Map<PlatformReadDto>(platformItem));
}
return NotFound();
}
[HttpPost]
public async Task<ActionResult<PlatformReadDto>> CreatePlatform(PlatformCreateDto platformCreateDto)
{
var platformModel = _mapper.Map<Platform>(platformCreateDto);
_repository.CreatePlatform(platformModel);
_repository.SaveChanges();
var platformReadDto = _mapper.Map<PlatformReadDto>(platformModel);
// Send Sync Message
try
{
await _commandDataClient.SendPlatformToCommand(platformReadDto);
}
catch(Exception ex)
{
Console.WriteLine($"--> Could not send synchronously: {ex.Message}");
}
//Send Async Message
try
{
var platformPublishedDto = _mapper.Map<PlatformPublishedDto>(platformReadDto);
platformPublishedDto.Event = "Platform_Published";
_messageBusClient.PublishNewPlatform(platformPublishedDto);
}
catch (Exception ex)
{
Console.WriteLine($"--> Could not send asynchronously: {ex.Message}");
}
return CreatedAtRoute(nameof(GetPlatformById), new { Id = platformReadDto.Id}, platformReadDto);
}
}
}