Пример #1
0
        public void Delete(Guid Id)
        {
            try
            {
                _accountUserRepository.BeginTransaction();
                var data = _accountUserRepository.Get(x => x.Id == Id).SingleOrDefault();

                _accountUserRepository.Delete(data);

                _accountUserRepository.Save();
                _accountUserRepository.CommitTransaction();
            }
            catch (Exception ex)
            {
                _accountUserRepository.RollbackTransaction();
                throw ex;
            }
        }
        public async Task <Result <ListResult <GetEntersResponse> > > Handle(GetEnters request, CancellationToken cancellationToken)
        {
            try
            {
                if (!IsAdminOrAccountAdmin(request.RequestedEmail, request.AccountId))
                {
                    return(Result <ListResult <GetEntersResponse> > .AccessDenied("No access!"));
                }

                var result = await _logService.GetAll();

                var myLogs = result.Where(x =>
                                          x.AccountId == request.AccountId &&
                                          x.EventTypeId == 4 &&
                                          x.Action == "success" &&
                                          x.CreatedAt >= request.From &&
                                          x.CreatedAt <= request.To);

                var listResponse = new List <GetEntersResponse>();

                foreach (var log in myLogs)
                {
                    var res = listResponse.FirstOrDefault(x => x.Email == log.User.Email && x.GateName == log.Gate.Name && x.FirstUse.Date == log.CreatedAt.Date);
                    if (res == null)
                    {
                        listResponse.Add(new GetEntersResponse
                        {
                            Name            = $"{log.User.FirstName} {log.User.LastName}",
                            Email           = log.User.Email,
                            FirstUse        = log.CreatedAt,
                            Date            = log.CreatedAt.Date,
                            GateName        = log.Gate.Name,
                            LastUse         = log.CreatedAt,
                            IsUserOfAccount = await _accountUserRepository.Get(request.AccountId, log.UserId.Value) == null ? false : true
                        });
                    }
                    else
                    {
                        if (res.FirstUse > log.CreatedAt)
                        {
                            res.FirstUse = log.CreatedAt;
                        }
                        if (res.LastUse < log.CreatedAt)
                        {
                            res.LastUse = log.CreatedAt;
                        }
                    }
                }

                if (!String.IsNullOrWhiteSpace(request.Filtering))
                {
                    listResponse = listResponse.Where(x => x.Name.Contains(request.Filtering) || x.Email.Contains(request.Filtering) || x.GateName.Contains(request.Filtering)).ToList();
                }

                var response = new ListResult <GetEntersResponse>(listResponse, listResponse.Count());

                return(Result <ListResult <GetEntersResponse> > .Ok(response));
            }
            catch (Exception e)
            {
                return(Result <ListResult <GetEntersResponse> > .Failure(e.Message));
            }
        }