Ejemplo n.º 1
0
        public async Task <IActionResult> Lock([FromBody] OpenLockInfo openLockInfo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var            result        = new LockingResult();
            OpenLockResult lockingResult = new OpenLockResult();

            try
            {
                _logger.LogInformation($"User {openLockInfo.UserId} request {"Open"} Lock with Id= {openLockInfo.LockId}");

                Guid auditLogId = await _auditLogService.LogLockRequest(openLockInfo.LockId,
                                                                        openLockInfo.UserId, Model.LockCommand.Open);

                if (_userAccessService.HasAccess(openLockInfo.UserId, openLockInfo.LockId).Result)
                {
                    lockingResult = await _lockingService.OpenLock(openLockInfo.LockId);

                    var auditsult = await _auditLogService.LogLockResult(auditLogId, lockingResult);

                    if (lockingResult == OpenLockResult.Opened)
                    {
                        _logger.LogInformation($"Request {"Open"} for Lock with Id={openLockInfo.LockId} has been successful.");
                        return(Ok(new LockOpenSuccessful()));
                    }
                    else
                    {
                        _logger.LogWarning($"Request {"Open"} for Lock with Id={openLockInfo.LockId} has been faild.");
                        return(Ok(new LockOpenFailed()));
                    }
                }
                else
                {
                    var auditsult = await _auditLogService.LogLockResult(auditLogId, OpenLockResult.UserAccessDenied);

                    return(Ok(new UserAccessDenied()));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error in Lock Service API");
                return(Ok(new LockingResult(-1, "Internal Error")));
            }
        }