public async Task <ActionResult <IEnumerable <ResponseWorkerList> > > GetWorkers()
        {
            var workers = await _context.Workers
                          .Include(x => x.Speciality)
                          .ThenInclude(x => x.Department)
                          .ToListAsync();


            return(Ok(workers.Select(x => ResponseWorkerList.FromApiWorker(x, avatar: _getAvatar(x.AvatarUrl))).ToList()));
        }
        public async Task <ActionResult <Models.WorkerWithToken> > SignUp([FromBody] Models.RegisterModel registerModel)
        {
            var registerModelValidator = new RegisterModelValidator();
            var validationResult       = registerModelValidator.Validate(registerModel);

            if (!validationResult.IsValid)
            {
                return(UnprocessableEntity(validationResult.Errors.Select(x => new ValidatorException
                {
                    Field = x.PropertyName,
                    Message = x.ErrorCode,
                    Description = x.ErrorMessage
                }).ToList()));
            }

            var hashPassword = HashPassword.Hash(registerModel.Password);
            var existUser    = await _context.Workers.SingleOrDefaultAsync(x => x.Email == registerModel.Email && x.Password == hashPassword);

            if (existUser == null)
            {
                var newWorker = new Models.Worker
                {
                    FirstName    = registerModel.FirstName,
                    LastName     = registerModel.LastName,
                    Email        = registerModel.Email,
                    Password     = hashPassword,
                    SpecialityId = registerModel.SpecialityId
                };
                var token = JWTExtension.CreateToken(newWorker);
                _context.Workers.Add(newWorker);
                await _context.SaveChangesAsync();

                var registeredWorker = await _context.Workers
                                       .Include(x => x.Speciality)
                                       .ThenInclude(x => x.Department)
                                       .SingleOrDefaultAsync(x => x.Email == newWorker.Email && x.Password == newWorker.Password);

                return(Ok(new Models.WorkerWithToken
                {
                    Token = token,
                    Worker = ResponseWorkerList.FromApiWorker(registeredWorker, avatar: _getAvatar(registeredWorker.AvatarUrl)),
                }));
            }
            else
            {
                return(Conflict(new ExceptionInfo
                {
                    Message = AuthErrorsMessages.UserExist,
                    Description = "Сотрудник с таким Email и паролем уже существует"
                }));
            }
        }
        public async Task <ActionResult <Models.WorkerWithToken> > SignIn([FromBody] Models.AuthModel authModel)
        {
            var authModelValidator = new AuthModelValidator();
            var validationResult   = authModelValidator.Validate(authModel);

            if (!validationResult.IsValid)
            {
                return(UnprocessableEntity(validationResult.Errors.Select(x => new ValidatorException {
                    Field = x.PropertyName,
                    Message = x.ErrorCode,
                    Description = x.ErrorMessage
                }).ToList()));
            }

            var hashPassword = HashPassword.Hash(authModel.Password);
            var existUser    = _context.Workers
                               .Include(x => x.Speciality)
                               .ThenInclude(x => x.Department)
                               .SingleOrDefault(x => x.Email == authModel.Email && x.Password == hashPassword);

            if (existUser != null)
            {
                Models.File avatar;


                if (existUser.AvatarUrl != null)
                {
                    var fullFileName = existUser.AvatarUrl.Replace(Constans.ApiUrl + Constans.FileDownloadPart, string.Empty);
                    avatar = _context.Files.SingleOrDefault(x => x.Name == fullFileName);
                }
                else
                {
                    avatar = null;
                }


                var token = JWTExtension.CreateToken(existUser);
                return(Ok(new Models.WorkerWithToken {
                    Token = token,
                    Worker = ResponseWorkerList.FromApiWorker(existUser, avatar: avatar),
                }));
            }
            else
            {
                return(NotFound(new ExceptionInfo {
                    Message = AuthErrorsMessages.UserNotFound,
                    Description = "Сотрудник с таким Email и паролем не существует"
                }));
            }
        }
        public async Task <ActionResult <ResponseWorkerList> > GetWorker(int id)
        {
            var worker = await _context.Workers
                         .Include(x => x.Speciality)
                         .ThenInclude(x => x.Department)
                         .SingleOrDefaultAsync(x => x.Id == id);

            if (worker == null)
            {
                return(NotFound(new ExceptionInfo {
                    Message = WorkersErrorsMessages.WorkerNotFound,
                    Description = "Работяга не найден"
                }));
            }

            return(Ok(ResponseWorkerList.FromApiWorker(worker, avatar: _getAvatar(worker.AvatarUrl))));
        }