-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathAccountController.cs
More file actions
67 lines (58 loc) · 1.95 KB
/
AccountController.cs
File metadata and controls
67 lines (58 loc) · 1.95 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
using Contracts;
using Entities.Extensions;
using Entities.Models;
using Microsoft.AspNetCore.Mvc;
using System;
namespace AccountOwnerServer.Controllers
{
[Route("api/[controller]")]
public class AccountController : Controller
{
private ILoggerManager _logger;
private IRepositoryWrapper _repository;
public AccountController(ILoggerManager logger, IRepositoryWrapper repository)
{
_logger = logger;
_repository = repository;
}
[HttpGet]
public IActionResult GetAllAccounts()
{
try
{
var accounts = _repository.Account.GetAllAccounts();
_logger.LogInfo($"Returned all accounts from database.");
return Ok(accounts);
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside GetAllAccounts action: {ex}");
return StatusCode(500, "Internal server error");
}
}
[HttpPost]
public IActionResult CreateOwner([FromBody]Account account)
{
try
{
if (account.IsObjectNull())
{
_logger.LogError("Object sent from client is null.");
return BadRequest("Object is null");
}
if (!ModelState.IsValid)
{
_logger.LogError("Invalid object sent from client.");
return BadRequest("Invalid model object");
}
_repository.Account.CreateAccount(account);
return CreatedAtRoute("AccountById", new { id = account.Id }, account);
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside CreateAccount action: {ex}");
return StatusCode(500, "Internal server error");
}
}
}
}