1
+ using Conube . Domain ;
2
+ using Conube . Infrastructure ;
3
+ using Fepin . Foundation ;
4
+ using Fepin . Persistence ;
5
+ using System ;
6
+ using System . IO ;
7
+ using System . Linq ;
8
+ using System . Net ;
9
+ using System . Net . Http ;
10
+ using System . Threading . Tasks ;
11
+ using System . Web . Http ;
12
+ using System . Web . Http . Description ;
13
+
14
+ namespace Conube . App . Api . Frontend . Controllers
15
+ {
16
+ /// <summary>
17
+ /// Empresas
18
+ /// </summary>
19
+ [ Authorize ]
20
+ [ RoutePrefix ( "v1/companies" ) ]
21
+ public class CompaniesController : BaseAccountController
22
+ {
23
+ private readonly CompanyApplicationService _service ;
24
+ private readonly ICertificateService _certificateService ;
25
+
26
+ /// <summary>
27
+ /// Construtor padrão
28
+ /// </summary>
29
+ /// <param name="contaRepository">Repositório de acesso as Empresas</param>
30
+ /// <param name="empresaService">Serviço base para acesso as empresas</param>
31
+ /// <param name="certificateService">Serviço de certificado</param>
32
+ public CompaniesController (
33
+ IRepository < Account > contaRepository ,
34
+ CompanyApplicationService empresaService ,
35
+ ICertificateService certificateService )
36
+ : base ( contaRepository )
37
+ {
38
+ _service = empresaService ;
39
+ _certificateService = certificateService ;
40
+ }
41
+
42
+ /// <summary>
43
+ /// Listar as empresas ativas de uma conta
44
+ /// </summary>
45
+ /// <returns>Todas as empresas</returns>
46
+ /// <response code="200">Sucesso na requisição</response>
47
+ /// <response code="400">API Key da conta não é valida</response>
48
+ [ Route ( "" ) ]
49
+ [ HttpGet ]
50
+ [ ResponseType ( typeof ( CompanyCollectionResource ) ) ]
51
+ public async Task < IHttpActionResult > Get ( )
52
+ {
53
+ var account = this . ApiKey ;
54
+ if ( account == null || account . Description . IndexOf ( "api.nfe.io" ) < 0 )
55
+ return BadRequest ( "account api key is not valid" ) ;
56
+
57
+ var account_id = account . ParentId ;
58
+
59
+ return ResponseResult ( await _service . All ( account_id ) ) ;
60
+ }
61
+
62
+ /// <summary>
63
+ /// Obter os detalhes de uma empresa
64
+ /// </summary>
65
+ /// <param name="company_id_or_tax_number">ID da empresa ou Inscrição Federal (CNPJ)</param>
66
+ /// <returns>Todas os detalhes da empresa</returns>
67
+ /// <response code="200">Sucesso na requisição</response>
68
+ /// <response code="400">API Key da conta não é valida</response>
69
+ [ Route ( "{company_id_or_tax_number}" ) ]
70
+ [ HttpGet ]
71
+ [ ResponseType ( typeof ( CompanySingleResource ) ) ]
72
+ public async Task < IHttpActionResult > Get ( string company_id_or_tax_number )
73
+ {
74
+ var account = this . ApiKey ;
75
+ if ( account == null || account . Description . IndexOf ( "api.nfe.io" ) < 0 )
76
+ return BadRequest ( "account api key is not valid" ) ;
77
+
78
+ var account_id = account . ParentId ;
79
+
80
+ var result = default ( Result < CompanySingleResource > ) ;
81
+
82
+ if ( Cnpj . Validate ( company_id_or_tax_number ) )
83
+ {
84
+ result = await _service . One ( account_id , long . Parse ( company_id_or_tax_number . RemoveNonNumeric ( ) ) ) ;
85
+ }
86
+ else
87
+ {
88
+ result = await _service . One ( account_id , company_id_or_tax_number ) ;
89
+ }
90
+
91
+ return ResponseResult ( result ) ;
92
+ }
93
+
94
+ /// <summary>
95
+ /// Criar uma empresa
96
+ /// </summary>
97
+ /// <param name="item">Dados da empresa</param>
98
+ /// <returns>Todas os detalhes da empresa</returns>
99
+ /// <response code="201">Sucesso na criação da empresa</response>
100
+ /// <response code="400">API Key da conta não é valida</response>
101
+ /// <response code="409">Já existe uma empresa com o CNPJ informado</response>
102
+ [ Route ( "" ) ]
103
+ [ HttpPost ]
104
+ [ ResponseType ( typeof ( CompanySingleResource ) ) ]
105
+ public async Task < IHttpActionResult > Post ( CompanyResource item )
106
+ {
107
+ var account = this . ApiKey ;
108
+ if ( account == null || account . Description . IndexOf ( "api.nfe.io" ) < 0 )
109
+ return BadRequest ( "account api key is not valid" ) ;
110
+
111
+ var account_id = account . ParentId ;
112
+
113
+ var result = await _service . Create ( account_id , item ) ;
114
+
115
+ if ( result . Status == ResultStatusCode . OK )
116
+ {
117
+ return Created ( string . Format ( "/v1/companies/{0}" , result . ValueAsSuccess . Companies . Id ) , result . ValueAsSuccess ) ;
118
+ }
119
+
120
+ return ResponseResult ( result ) ;
121
+ }
122
+
123
+ /// <summary>
124
+ /// Atualizar uma empresa
125
+ /// </summary>
126
+ /// <param name="company_id">ID da empresa</param>
127
+ /// <param name="item">Dados da empresa</param>
128
+ /// <returns>Todos os detalhes de uma empresa</returns>
129
+ /// <response code="200">Sucesso na atualização da empresa</response>
130
+ /// <response code="400">API Key da conta não é valida</response>
131
+ /// <response code="400">Algum parametro informado não é válido</response>
132
+ [ Route ( "{company_id}" ) ]
133
+ [ HttpPut ]
134
+ [ ResponseType ( typeof ( CompanySingleResource ) ) ]
135
+ public async Task < IHttpActionResult > Put ( string company_id , CompanyResource item )
136
+ {
137
+ var account = this . ApiKey ;
138
+ if ( account == null || account . Description . IndexOf ( "api.nfe.io" ) < 0 )
139
+ return BadRequest ( "account api key is not valid" ) ;
140
+
141
+ var account_id = account . ParentId ;
142
+
143
+ return ResponseResult ( await _service . Save ( account_id , company_id , item ) ) ;
144
+ }
145
+
146
+ /// <summary>
147
+ /// Excluir uma empresa
148
+ /// </summary>
149
+ /// <param name="company_id">ID da empresa</param>
150
+ /// <returns>Todos os detalhes de uma empresa</returns>
151
+ /// <response code="200">Sucesso na remoção da empresa</response>
152
+ /// <response code="400">API Key da conta não é valida</response>
153
+ /// <response code="400">Algum parametro informado não é válido</response>
154
+ /// <response code="404">empresa não foi encontrada</response>
155
+ [ Route ( "{company_id}" ) ]
156
+ [ HttpDelete ]
157
+ public async Task < IHttpActionResult > Delete ( string company_id )
158
+ {
159
+ var account = this . ApiKey ;
160
+ if ( account == null || account . Description . IndexOf ( "api.nfe.io" ) < 0 )
161
+ return BadRequest ( "account api key is not valid" ) ;
162
+
163
+ var account_id = account . ParentId ;
164
+
165
+ return ResponseResult ( await _service . Delete ( account_id , company_id ) ) ;
166
+ }
167
+
168
+ /// <summary>
169
+ /// Upload do certificado digital da empresa usando o codificação multipart/form-data.
170
+ /// </summary>
171
+ /// <param name="company_id">ID da empresa</param>
172
+ /// <returns>Todos os detalhes de uma empresa</returns>
173
+ /// <response code="200">Sucesso na atualização da empresa</response>
174
+ /// <response code="400">API Key da conta não é valida</response>
175
+ /// <response code="400">Algum parametro informado não é válido</response>
176
+ /// <response code="404">Empresa não foi encontrada</response>
177
+ /// <response code="415">Nenhum arquivo foi encontrado na requisição</response>
178
+ [ Route ( "{company_id}/certificate" ) ]
179
+ [ HttpPost ]
180
+ [ ResponseType ( typeof ( string ) ) ]
181
+ public async Task < IHttpActionResult > Post ( string company_id )
182
+ {
183
+ var account = this . ApiKey ;
184
+ if ( account == null || account . Description . IndexOf ( "api.nfe.io" ) < 0 )
185
+ return BadRequest ( "account api key is not valid" ) ;
186
+
187
+ var account_id = account . ParentId ;
188
+
189
+ if ( Request . Content . IsMimeMultipartContent ( ) == false )
190
+ return StatusCode ( HttpStatusCode . UnsupportedMediaType ) ;
191
+
192
+ var provider = await Request . Content . ReadAsMultipartAsync ( new MultipartFormDataStreamProvider ( Path . GetTempPath ( ) ) ) ;
193
+ var files = provider . FileData ;
194
+ if ( files . Any ( ) == false )
195
+ return BadRequest ( "certificate file not found" ) ;
196
+
197
+ var filePath = files . First ( ) . LocalFileName ;
198
+ var password = provider . FormData [ "password" ] ;
199
+
200
+ return ResponseResult ( await _service . SaveCertificate ( _certificateService , account_id , company_id , filePath , password ) ) ;
201
+ }
202
+ }
203
+ }
0 commit comments