Advertisement
simonradev

AccountController

May 8th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. namespace MonitoringSystem.Hubs.Controllers
  2. {
  3.     using System.Linq;
  4.     using Microsoft.AspNetCore.Authorization;
  5.     using Microsoft.AspNetCore.Mvc;
  6.     using Microsoft.EntityFrameworkCore;
  7.  
  8.     using AutoMapper;
  9.     using Swashbuckle.AspNetCore.SwaggerGen;
  10.  
  11.     using MonitoringSystem.DataLayer.EFCore.Contracts;
  12.     using MonitoringSystem.DataModels;
  13.     using MonitoringSystem.Hubs.Dtos;
  14.     using System.Collections.Generic;
  15.  
  16.     [Authorize]
  17.     public class AccountController : ApiControllerBase
  18.     {
  19.         private readonly IUserRepository _userRepository;
  20.         private readonly IMapper _mapper;
  21.  
  22.         public AccountController(
  23.             IUserRepository userRepository,
  24.             IMapper mapper)
  25.         {
  26.             this._userRepository = userRepository;
  27.             this._mapper = mapper;
  28.         }
  29.  
  30.         [HttpGet]
  31.         [Route("/margins")]
  32.         [ApiExplorerSettings(GroupName = "v1")]
  33.         [SwaggerOperation("GetLoggedUserMarketMargins")]
  34.         [SwaggerResponse(200, type: typeof(JsonResult))]
  35.         [SwaggerResponse(400, type: typeof(IActionResult))]
  36.         [SwaggerResponse(statusCode: 500, type: typeof(IActionResult), description: "frfrfr")]
  37.         public IActionResult GetMarketMargins()
  38.         {
  39.             if (int.TryParse(User.Identity.Name, out var userIdFromToken))
  40.             {
  41.                 using (_userRepository)
  42.                 {
  43.                     var userMarketMargins = _userRepository.GetMarketMargins(
  44.                                                                 userIdFromToken,
  45.                                                                 mm => mm.Include(x => x.User)
  46.                                                                         .Include(x => x.OddType))
  47.                                                            .ToArray();
  48.                     return Ok(userMarketMargins);
  49.                 }
  50.             }
  51.  
  52.             return Unauthorized();
  53.         }
  54.  
  55.         [HttpPost]
  56.         [Route("/margins")]
  57.         [ApiExplorerSettings(GroupName = "v1")]
  58.         [SwaggerOperation("GetLoggedUserMarketMargins")]
  59.         [SwaggerResponse(200, type: typeof(JsonResult))]
  60.         [SwaggerResponse(400, type: typeof(IActionResult))]
  61.         [SwaggerResponse(statusCode: 500, type: typeof(IActionResult), description: "frfrfr")]
  62.         public IActionResult SaveMarketMargins([FromBody] IEnumerable<UserMarketMarginsDto> userMarketMarginsDto)
  63.         {
  64.             if (userMarketMarginsDto == null)
  65.             {
  66.                 return BadRequest("Market Margins must be provided in order to save!");
  67.             }
  68.  
  69.             if (int.TryParse(User.Identity.Name, out var userIdFromToken))
  70.             {
  71.                 var userMarketMargins = _mapper.Map<IEnumerable<UserMarketMargins>>(userMarketMarginsDto)
  72.                                                .Select(umm =>
  73.                                                {
  74.                                                    umm.UserID = userIdFromToken;
  75.                                                    return umm;
  76.                                                });
  77.                
  78.                 using (_userRepository)
  79.                 {
  80.                     _userRepository.SaveMarketMargin(userIdFromToken, userMarketMargins);
  81.                     return Ok(userMarketMargins);
  82.                 }
  83.             }
  84.  
  85.             return Unauthorized();
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement