Skip to content

Commit 3133187

Browse files
Seção 10: Finalizando Eventos e Lotes
1 parent df146aa commit 3133187

46 files changed

Lines changed: 775 additions & 183 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Back/src/BrnEventos.API/Controllers/EventosController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public async Task<IActionResult> Delete(int id)
110110
var evento = await _eventoService.GetEventoByIdAsync(id, true);
111111
if (evento == null) return NoContent();
112112

113-
return await _eventoService.DeleteEvento(id)
114-
? Ok(new { menssage = "Deletado"})
113+
return await _eventoService.DeleteEvento(id)
114+
? Ok(new { menssage = "Deletado" })
115115
: throw new Exception("Ocorreu um erro não específico ao tentar deletar o Evento");
116116
}
117117
catch (Exception ex)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using BrnEventos.Application.Contratos;
3+
using System.Threading.Tasks;
4+
using System;
5+
using Microsoft.AspNetCore.Http;
6+
using BrnEventos.Application.Dtos;
7+
8+
namespace BrnEventos.API.Controllers
9+
{
10+
[ApiController]
11+
[Route("api/[controller]")]
12+
public class LotesController : ControllerBase
13+
{
14+
private readonly ILoteService _loteService;
15+
16+
public LotesController(ILoteService LoteService)
17+
{
18+
_loteService = LoteService;
19+
}
20+
21+
[HttpGet("{eventoId}")]
22+
public async Task<IActionResult> Get(int eventoId)
23+
{
24+
try
25+
{
26+
var lotes = await _loteService.GetLotesByEventoIdAsync(eventoId);
27+
if (lotes == null) return NoContent();
28+
return Ok(lotes);
29+
}
30+
catch (Exception ex)
31+
{
32+
return this.StatusCode(StatusCodes.Status500InternalServerError,
33+
$"Erro ao tentar recuperar Lotes! Erro: {ex.Message}");
34+
}
35+
}
36+
37+
[HttpPut("{eventoId}")]
38+
public async Task<IActionResult> SaveLotes(int eventoId, LoteDto[] models)
39+
{
40+
try
41+
{
42+
var lotes = await _loteService.SaveLotes(eventoId, models);
43+
if (lotes == null) return NoContent();
44+
45+
return Ok(lotes);
46+
}
47+
catch (Exception ex)
48+
{
49+
return this.StatusCode(StatusCodes.Status500InternalServerError,
50+
$"Erro ao tentar salvar Lotes! Erro: {ex.Message}");
51+
}
52+
}
53+
54+
[HttpDelete("{eventoId}/{loteId}")]
55+
public async Task<IActionResult> Delete(int eventoId, int loteId)
56+
{
57+
try
58+
{
59+
var lote = await _loteService.GetLoteByIdsAsync(eventoId, loteId);
60+
if (lote == null) return NoContent();
61+
62+
return await _loteService.DeleteLote(lote.EventoId, lote.Id)
63+
? Ok(new { menssage = "Lote Deletado" })
64+
: throw new Exception("Ocorreu um erro não específico ao tentar deletar o Lote!");
65+
}
66+
catch (Exception ex)
67+
{
68+
return this.StatusCode(StatusCodes.Status500InternalServerError,
69+
$"Erro ao tentar deletar Lotes! Erro: {ex.Message}");
70+
}
71+
}
72+
}
73+
}

Back/src/BrnEventos.API/Startup.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,26 @@ public void ConfigureServices(IServiceCollection services)
3232
);
3333
services.AddControllers()
3434
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling =
35-
Newtonsoft.Json.ReferenceLoopHandling.Ignore
35+
Newtonsoft.Json.ReferenceLoopHandling.Ignore
3636
);
3737

38-
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
38+
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
3939

4040
services.AddScoped<IEventoService, EventoService>();
41+
services.AddScoped<ILoteService, LoteService>();
42+
4143
services.AddScoped<IGeralPersist, GeralPersist>();
4244
services.AddScoped<IEventoPersist, EventoPersist>();
45+
services.AddScoped<ILotePersist, LotePersist>();
4346

4447
services.AddCors();
4548
services
4649
.AddSwaggerGen(c =>
4750
{
4851
c
4952
.SwaggerDoc("v1",
50-
new OpenApiInfo {
53+
new OpenApiInfo
54+
{
5155
Title = "BrnEventos.API",
5256
Version = "v1"
5357
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using BrnEventos.Application.Dtos;
3+
using BrnEventos.Domain;
4+
5+
namespace BrnEventos.Application.Contratos
6+
{
7+
public interface ILoteService
8+
{
9+
Task<LoteDto[]> SaveLotes(int eventoId, LoteDto[] models);
10+
Task<bool> DeleteLote(int eventoId, int loteId);
11+
12+
Task<LoteDto[]> GetLotesByEventoIdAsync(int eventoId);
13+
Task<LoteDto> GetLoteByIdsAsync(int eventoId, int loteId);
14+
}
15+
}

Back/src/BrnEventos.Application/Dtos/LoteDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace BrnEventos.Application.Dtos
22
{
33
public class LoteDto
4-
{
4+
{
55
public int Id { get; set; }
66
public string Nome { get; set; }
77
public decimal Preco { get; set; }

Back/src/BrnEventos.Application/Dtos/PalestranteDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace BrnEventos.Application.Dtos
44
{
55
public class PalestranteDto
6-
{
6+
{
77
public int Id { get; set; }
88
public string Nome { get; set; }
99
public string MiniCurriculo { get; set; }

Back/src/BrnEventos.Application/Dtos/RedeSocialDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace BrnEventos.Application.Dtos
77
{
88
public class RedeSocialDto
9-
{
9+
{
1010
public int Id { get; set; }
1111
public string Nome { get; set; }
1212
public string URL { get; set; }

Back/src/BrnEventos.Application/EventoService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task<bool> DeleteEvento(int eventoId)
8383
if (evento == null)
8484
throw new Exception("Evento não encontrado!");
8585

86-
_geralPersist.Delete<Evento> (evento);
86+
_geralPersist.Delete<Evento>(evento);
8787
return await _geralPersist.SaveChangesAsync();
8888
}
8989
catch (Exception ex)
@@ -101,8 +101,8 @@ public async Task<EventoDto[]> GetAllEventosAsync(bool includePalestrantes = fal
101101

102102
var eventosRetorno = new List<EventoDto>();
103103

104-
var resultado = _mapper.Map<EventoDto[]>(eventos);
105-
104+
var resultado = _mapper.Map<EventoDto[]>(eventos);
105+
106106
return resultado;
107107
}
108108
catch (Exception ex)
@@ -118,8 +118,8 @@ public async Task<EventoDto[]> GetAllEventosByTemaAsync(string tema, bool includ
118118
var eventos = await _eventoPersist.GetAllEventosByTemaAsync(tema, includePalestrantes);
119119
if (eventos == null) return null;
120120

121-
var resultado = _mapper.Map<EventoDto[]>(eventos);
122-
121+
var resultado = _mapper.Map<EventoDto[]>(eventos);
122+
123123
return resultado;
124124
}
125125
catch (Exception ex)
@@ -135,8 +135,8 @@ public async Task<EventoDto> GetEventoByIdAsync(int eventoId, bool includePalest
135135
var evento = await _eventoPersist.GetEventoByIdAsync(eventoId, includePalestrantes);
136136
if (evento == null) return null;
137137

138-
var resultado = _mapper.Map<EventoDto>(evento);
139-
138+
var resultado = _mapper.Map<EventoDto>(evento);
139+
140140
return resultado;
141141
}
142142
catch (Exception ex)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using BrnEventos.Application.Contratos;
5+
using BrnEventos.Domain;
6+
using BrnEventos.Persistence.Contratos;
7+
using BrnEventos.Application.Dtos;
8+
using AutoMapper;
9+
using System.Linq;
10+
11+
namespace BrnEventos.Application
12+
{
13+
public class LoteService : ILoteService
14+
{
15+
private readonly IGeralPersist _geralPersist;
16+
17+
private readonly ILotePersist _lotePersist;
18+
private readonly IMapper _mapper;
19+
20+
public LoteService(
21+
IGeralPersist geralPersist,
22+
ILotePersist lotePersist,
23+
IMapper mapper
24+
)
25+
{
26+
_geralPersist = geralPersist;
27+
_lotePersist = lotePersist;
28+
_mapper = mapper;
29+
}
30+
31+
public async Task AddLote(int eventoId, LoteDto model)
32+
{
33+
try
34+
{
35+
var lote = _mapper.Map<Lote>(model);
36+
lote.EventoId = eventoId;
37+
38+
_geralPersist.Add<Lote>(lote);
39+
40+
await _geralPersist.SaveChangesAsync();
41+
}
42+
catch (Exception ex)
43+
{
44+
throw new Exception(ex.Message);
45+
}
46+
}
47+
48+
public async Task<LoteDto[]> SaveLotes(int eventoId, LoteDto[] models)
49+
{
50+
try
51+
{
52+
var lotes = await _lotePersist.GetLotesByEventoIdAsync(eventoId);
53+
if (lotes == null) return null;
54+
55+
foreach (var model in models)
56+
{
57+
58+
if (model.Id == 0)
59+
{
60+
await AddLote(eventoId, model);
61+
}
62+
else
63+
{
64+
var lote = lotes.FirstOrDefault(lote => lote.Id == model.Id);
65+
model.EventoId = eventoId;
66+
67+
_mapper.Map(model, lote);
68+
69+
_geralPersist.Update<Lote>(lote);
70+
await _geralPersist.SaveChangesAsync();
71+
}
72+
}
73+
74+
var loteRetorno = await _lotePersist.GetLotesByEventoIdAsync(eventoId);
75+
76+
return _mapper.Map<LoteDto[]>(loteRetorno);
77+
}
78+
catch (Exception ex)
79+
{
80+
throw new Exception(ex.Message);
81+
}
82+
}
83+
84+
public async Task<bool> DeleteLote(int eventoId, int loteId)
85+
{
86+
try
87+
{
88+
var lote = await _lotePersist.GetLoteByIdsAsync(eventoId, loteId);
89+
if (lote == null) throw new Exception("Lote não encontrado!");
90+
91+
_geralPersist.Delete<Lote>(lote);
92+
return await _geralPersist.SaveChangesAsync();
93+
}
94+
catch (Exception ex)
95+
{
96+
throw new Exception(ex.Message);
97+
}
98+
}
99+
100+
public async Task<LoteDto[]> GetLotesByEventoIdAsync(int eventoId)
101+
{
102+
try
103+
{
104+
var lotes = await _lotePersist.GetLotesByEventoIdAsync(eventoId);
105+
if (lotes == null) return null;
106+
107+
var resultado = _mapper.Map<LoteDto[]>(lotes);
108+
109+
return resultado;
110+
}
111+
catch (Exception ex)
112+
{
113+
throw new Exception(ex.Message);
114+
}
115+
}
116+
117+
public async Task<LoteDto> GetLoteByIdsAsync(int eventoId, int loteId)
118+
{
119+
try
120+
{
121+
var lote = await _lotePersist.GetLoteByIdsAsync(eventoId, loteId);
122+
if (lote == null) return null;
123+
124+
var resultado = _mapper.Map<LoteDto>(lote);
125+
126+
return resultado;
127+
}
128+
catch (Exception ex)
129+
{
130+
throw new Exception(ex.Message);
131+
}
132+
}
133+
}
134+
}

Back/src/BrnEventos.Persistence/Contratos/IEventoPersist.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace BrnEventos.Persistence.Contratos
55
{
66
public interface IEventoPersist
77
{
8-
//EVENTOS
98
Task<Evento[]> GetAllEventosByTemaAsync(string tema, bool includePalestrantes = false);
109
Task<Evento[]> GetAllEventosAsync(bool includePalestrantes = false);
1110
Task<Evento> GetEventoByIdAsync(int eventoId, bool includePalestrantes = false);

0 commit comments

Comments
 (0)