1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System.Linq;
- using System.Transactions;
- using Microsoft.AspNetCore.Mvc;
- using UnivateProperties_API.Containers.Users;
- using UnivateProperties_API.Helpers;
- using UnivateProperties_API.Model.Users;
- using UnivateProperties_API.Repository;
-
- namespace User_API.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class AgentController : ControllerBase
- {
- private readonly IRepository<Agent> _Repo;
-
- public AgentController(IRepository<Agent> repo)
- {
- _Repo = repo;
- }
-
- [HttpGet]
- public IActionResult Get()
- {
- return new OkObjectResult(_Repo.GetAll());
- }
-
- [HttpGet("GetByAgency/{id}")]
- public IActionResult GetByAgency(int id)
- {
- return new OkObjectResult(_Repo.Get(x => x.AgencyId == id).ToList());
- }
-
- [HttpGet("{id}")]
- public IActionResult Get(int id)
- {
- return new OkObjectResult(_Repo.Get(x => x.Id == id));
- }
-
- [HttpGet("single/{userId}")]
- public IActionResult GetByUserId(int userId)
- {
- return new OkObjectResult(_Repo.Get(x => x.UserId == userId));
- }
-
- [HttpPost()]
- public IActionResult Post([FromBody] AgentDto agentDto)
- {
- using (var scope = new TransactionScope())
- {
- Agent agent = agentDto.Agent;
- MyCommon.CreatePasswordHash(agentDto.Password, out byte[] passwordHash, out byte[] passwordSalt);
-
- agent.User.PasswordHash = passwordHash;
- agent.User.PasswordSalt = passwordSalt;
- _Repo.Insert(agent);
- scope.Complete();
- return CreatedAtAction(nameof(Get), new { id = agentDto.Agent.Id }, agentDto.Agent);
- }
- }
-
- [HttpPost("AgentFromUser")]
- public IActionResult Post(int id, [FromBody] Agent agent)
- {
- using (var scope = new TransactionScope())
- {
- _Repo.Insert(agent);
- scope.Complete();
- return CreatedAtAction(nameof(Get), new { id = agent.Id }, agent);
- }
- }
-
- [HttpPut()]
- public IActionResult Put([FromBody] Agent agent)
- {
- if (agent != null)
- {
- using (var scope = new TransactionScope())
- {
- _Repo.Update(agent);
- scope.Complete();
- return new OkResult();
- }
- }
- return new NoContentResult();
- }
-
- [HttpDelete("{id}")]
- public IActionResult Delete(int id)
- {
- _Repo.RemoveAtId(id);
- return new OkResult();
- }
- }
- }
|