DocSpace-buildtools/products/ASC.People/Controllers/PeopleController.cs

71 lines
2.4 KiB
C#
Raw Normal View History

2019-05-27 09:46:04 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-06-13 15:01:29 +00:00
using ASC.Common.Web;
2019-05-27 09:46:04 +00:00
using ASC.Core;
using ASC.Core.Users;
using ASC.Web.Api.Models;
2019-05-27 12:49:48 +00:00
using ASC.Web.Api.Routing;
using Microsoft.AspNetCore.Mvc;
namespace ASC.Employee.Core.Controllers
{
2019-05-27 12:49:48 +00:00
[DefaultRoute]
[ApiController]
public class PeopleController : ControllerBase
{
2019-06-13 15:01:29 +00:00
public Common.Logging.LogManager LogManager { get; }
public PeopleController(ASC.Common.Logging.LogManager logManager)
{
LogManager = logManager;
}
[FormatRoute, FormatRoute(false)]
2019-05-27 09:46:04 +00:00
public IEnumerable<EmployeeWraper> GetAll()
{
return GetByStatus(EmployeeStatus.Active);
}
2019-05-27 12:49:48 +00:00
[FormatRoute("status/{status}")]
2019-05-27 09:46:04 +00:00
public IEnumerable<EmployeeWraper> GetByStatus(EmployeeStatus status)
{
if (CoreContext.Configuration.Personal) throw new Exception("Method not available");
var query = CoreContext.UserManager.GetUsers(status).AsEnumerable();
2019-06-13 09:04:46 +00:00
return query.Select(x => new EmployeeWraperFull(x, HttpContext));
2019-05-27 09:46:04 +00:00
}
2019-05-27 12:49:48 +00:00
2019-06-13 15:01:29 +00:00
[FormatRoute("@self", Order = 1), FormatRoute("@self", false, Order = 1)]
2019-05-27 12:49:48 +00:00
public EmployeeWraper Self()
{
2019-06-13 09:04:46 +00:00
return new EmployeeWraperFull(CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID), HttpContext);
2019-05-27 12:49:48 +00:00
}
2019-06-13 15:01:29 +00:00
[FormatRoute("{username}", Order = 2)]
public EmployeeWraperFull GetById(string username)
{
if (CoreContext.Configuration.Personal) throw new MethodAccessException("Method not available");
var user = CoreContext.UserManager.GetUserByUserName(username);
if (user.ID == Constants.LostUser.ID)
{
Guid userId;
if (Guid.TryParse(username, out userId))
{
user = CoreContext.UserManager.GetUsers(userId);
}
else
{
LogManager.Get("ASC.Api").Error(string.Format("Account {0} сould not get user by name {1}", SecurityContext.CurrentAccount.ID, username));
}
}
if (user.ID == Constants.LostUser.ID)
{
throw new ItemNotFoundException("User not found");
}
return new EmployeeWraperFull(user);
}
}
}