Exemplo n.º 1
0
        public async Task <PagedItems> GetUserClients(string userId, int page, int pageSize, string urlLink)
        {
            var userExists = await Context.GetUserCollection().Find(x => x.Id.Equals(userId)).AnyAsync();

            if (!userExists)
            {
                CustomException.ThrowNotFoundException();
            }

            var clientsDto           = new List <ICommonDto>();
            var skip                 = pageSize * (page - 1);
            var totalNumberOfClients = await Context.GetClientCollection().CountAsync(x => x.UserId.Equals(userId));

            using (
                var cursor =
                    await Context.GetClientCollection()
                    .Find(x => x.UserId.Equals(userId))
                    .SortBy(x => x.Id)
                    .Skip(skip)
                    .Limit(pageSize)
                    .ToCursorAsync())
            {
                while (await cursor.MoveNextAsync())
                {
                    clientsDto.AddRange(cursor.Current.Select(client => _clientFactory.GetModel <ClientDto>(client)));
                }
            }
            return(CreatePagedItems(clientsDto, urlLink, page, pageSize, totalNumberOfClients));
        }
Exemplo n.º 2
0
        public async Task <ICommonDto> UpdateRole(string id, string name)
        {
            var roleExists = await Context.GetRoleCollection().Find(x => x.Id.Equals(id)).AnyAsync();

            if (!roleExists)
            {
                CustomException.ThrowNotFoundException();
            }

            var roleNameExists = await Context.GetRoleCollection().Find(x => x.Name.Equals(name)).AnyAsync();

            if (roleNameExists)
            {
                CustomException.ThrowBadRequestException("Name is taken.");
            }

            var update = Builders <IdentityRole> .Update.Set(x => x.Name, name);

            var updateResult = await Context.GetRoleCollection().UpdateOneAsync(x => x.Id.Equals(id), update);

            if (!updateResult.IsAcknowledged)
            {
                CustomException.ThrowBadRequestException("Update failed.");
            }

            var updatedRole = await Context.GetRoleCollection().Find(x => x.Id.Equals(id)).SingleAsync();

            return(_roleFactory.GetModel <RoleDto>(updatedRole));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> GetErrorLogsByUser(string userName, [FromUri] Pagination paginationModel)
        {
            if (await UserManager.FindByEmailAsync(userName) == null)
            {
                CustomException.ThrowNotFoundException($"There is no user {userName}.");
            }

            return(Ok(await ErrorLogService.GetAllByUserAsync(userName, paginationModel.Page, paginationModel.PageSize)));
        }
Exemplo n.º 4
0
        private async Task MovieExists(string id)
        {
            var movieExists = await Context.GetMovieCollection().Find(x => x.Id.Equals(id)).AnyAsync();

            if (!movieExists)
            {
                CustomException.ThrowNotFoundException();
            }
        }
Exemplo n.º 5
0
        public async Task <IdentityRoleDto> GetByName(string name)
        {
            if (!await RoleExistsAsync(name))
            {
                CustomException.ThrowNotFoundException($"There is no role: {name}.");
            }

            var role = await _roleManager.FindByNameAsync(name);

            return(_roleModelFactory.GetModel(role));
        }
Exemplo n.º 6
0
        public override async Task RemoveAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no role with id = {id}.");
            }

            var role = await _roleManager.FindByIdAsync((string)id);

            await _roleManager.DeleteAsync(role);
        }
Exemplo n.º 7
0
        public override async Task <IdentityRoleDto> GetByIdAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no role with id = {id}.");
            }

            var role = await _roleManager.FindByIdAsync((string)id);

            return(_roleModelFactory.GetModel(role));
        }
Exemplo n.º 8
0
        public override async Task <ClientDto> GetByIdAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no client with clientId = {id}.");
            }

            var client = await UnitOfWork.ClientRepository.FindAsync(id);

            return(ModelFactory.GetModel <ClientDto>(client));
        }
Exemplo n.º 9
0
        public async Task <ClientByUserNameDto> GetMyClientAsync(string userName, string clientId)
        {
            if (!await ExistsAsync(clientId))
            {
                CustomException.ThrowNotFoundException($"There is no client with id = {clientId}.");
            }

            var client = await UnitOfWork.ClientRepository.SingleOrDefaultAsync(x => x.Username.Equals(userName) && x.Id.Equals(clientId));

            return(ModelFactory.GetModel <ClientByUserNameDto>(client));
        }
Exemplo n.º 10
0
        public override async Task RemoveAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no client with clientId = {id}.");
            }

            var clientDomain = await UnitOfWork.ClientRepository.FindAsync(id);

            await UnitOfWork.ClientRepository.RemoveAsync(clientDomain);
        }
Exemplo n.º 11
0
        public async Task <RefreshTokenDto> GetByClientIdAsync(object clientId)
        {
            if (!await ExistsByClientIdAsync(clientId))
            {
                CustomException.ThrowNotFoundException($"There is no refresh token with clientId = {clientId}.");
            }

            var token = await UnitOfWork.RefreshTokenRepository.SingleOrDefaultAsync(x => x.ClientId == (string)clientId);

            return(ModelFactory.GetModel <RefreshTokenDto>(token));
        }
Exemplo n.º 12
0
        public async Task <CityDto> GetByCityNameAsync(string cityName)
        {
            if (!await ExistsAsync(cityName))
            {
                CustomException.ThrowNotFoundException($"There is no city with cityName = {cityName}.");
            }

            var city = await UnitOfWork.CityRepository.SingleOrDefaultAsync(x => x.CityName.Equals(cityName));

            return(ModelFactory.GetModel <CityDto>(city));
        }
Exemplo n.º 13
0
        public override async Task <CityDto> GetByIdAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no city with id = {id}.");
            }

            var city = await UnitOfWork.CityRepository.FindAsync(id);

            return(ModelFactory.GetModel <CityDto>(city));
        }
Exemplo n.º 14
0
        public override async Task RemoveAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no log with id = {id}.");
            }

            var apiLogEntryDomain = await UnitOfWork.ActivityLogRepository.FindAsync(id);

            await UnitOfWork.ActivityLogRepository.RemoveAsync(apiLogEntryDomain);
        }
Exemplo n.º 15
0
        public override async Task <ErrorLogDto> GetByIdAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no log with id = {id}.");
            }

            var log = await UnitOfWork.ErrorLogRepository.FindAsync((long)id);

            return(ModelFactory.GetModel <ErrorLogDto>(log));
        }
Exemplo n.º 16
0
        public async Task <StateDto> GetByNameAsync(string stateName)
        {
            if (!await ExistsByNameAsync(stateName))
            {
                CustomException.ThrowNotFoundException($"There is no state with name {stateName}.");
            }

            var state = await UnitOfWork.StateRepository.SingleOrDefaultAsync(x => x.StateName.Equals(stateName));

            return(ModelFactory.GetModel <StateDto>(state));
        }
Exemplo n.º 17
0
        public async Task <IHttpActionResult> GetUserClients(string userName, [FromUri] Pagination paginationModel)
        {
            var user = await UserManager.FindByEmailAsync(userName);

            if (user == null)
            {
                CustomException.ThrowNotFoundException($"User {userName} doesn't exists.");
            }

            return(Ok(await ClientService.GetUserClients(userName, paginationModel.Page, paginationModel.PageSize)));
        }
Exemplo n.º 18
0
        public override async Task RemoveAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no state with id = {id}.");
            }

            var stateDomain = await UnitOfWork.StateRepository.FindAsync(id);

            await UnitOfWork.StateRepository.RemoveAsync(stateDomain);
        }
Exemplo n.º 19
0
        public override async Task <StateDto> GetByIdAsync(object id)
        {
            if (!await ExistsAsync(id))
            {
                CustomException.ThrowNotFoundException($"There is no state with id = {id}.");
            }

            var state = await UnitOfWork.StateRepository.FindAsync(id);

            return(ModelFactory.GetModel <StateDto>(state));
        }
Exemplo n.º 20
0
        public override async Task <StateDto> Update(StatePutDto dtoModel)
        {
            if (!await ExistsAsync(dtoModel.Id))
            {
                CustomException.ThrowNotFoundException($"State with id: {dtoModel.Id} doesn't exist.");
            }

            var stateDomain = ModelFactory.GetModel <State>(dtoModel);
            await UnitOfWork.StateRepository.Update(stateDomain);

            return(await GetByIdAsync(dtoModel.Id));
        }
Exemplo n.º 21
0
        public override async Task <CityDto> AddAsync(CityPostDto dtoModel)
        {
            if (!await UnitOfWork.StateRepository.AnyAsync(x => x.Id == dtoModel.StateId))
            {
                CustomException.ThrowNotFoundException($"State with id = {dtoModel.StateId} doesn't exist.");
            }

            var cityDomain = ModelFactory.GetModel <City>(dtoModel);
            var newEntity  = await UnitOfWork.CityRepository.AddAsync(cityDomain);

            return(await GetByIdAsync(newEntity.Id));
        }
Exemplo n.º 22
0
        public async Task <IHttpActionResult> PostEventAsList(EventPostAsListDto eventList)
        {
            var cityExistance = await EventService.CheckCityExistance(eventList.Events);

            if (!string.IsNullOrEmpty(cityExistance))
            {
                CustomException.ThrowNotFoundException($"Cities with id = {cityExistance} don't exist.");
            }

            var newEventList = await EventService.AddListAsync(eventList);

            return(Created("EventRoute", newEventList));
        }
Exemplo n.º 23
0
        public override async Task <ICommonDto> GetById(string id)
        {
            var roleExists = await Context.GetRoleCollection().Find(x => x.Id.Equals(id)).AnyAsync();

            if (!roleExists)
            {
                CustomException.ThrowNotFoundException();
            }

            var role = await Context.GetRoleCollection().Find(x => x.Id.Equals(id)).SingleAsync();

            return(_roleFactory.GetModel <RoleDto>(role));
        }
Exemplo n.º 24
0
        public override async Task <EventDto> Update(EventPutDto dtoModel)
        {
            if (!await UnitOfWork.CityRepository.AnyAsync(x => x.Id == dtoModel.CityId))
            {
                CustomException.ThrowNotFoundException($"City with id = {dtoModel.CityId} doesn't exist.");
            }

            var eventDomain = ModelFactory.GetModel <Event>(dtoModel);

            eventDomain.Categories = await CheckCategories(dtoModel.Categories);

            await UnitOfWork.EventRepository.Update(eventDomain);

            return(await GetByIdAsync(dtoModel.Id));
        }
Exemplo n.º 25
0
        public override async Task <ClientDto> Update(ClientDto dtoModel)
        {
            if (!await ExistsAsync(dtoModel.Id))
            {
                CustomException.ThrowNotFoundException($"There is no client with clientId = {dtoModel.Id}.");
            }

            var hashedClientSecret = GetHash(dtoModel.ClientSecret);
            var clientDomain       = ModelFactory.GetModel <Domain.Entity.Client.Client>(dtoModel);

            clientDomain.ClientSecret = hashedClientSecret;
            await UnitOfWork.ClientRepository.Update(clientDomain);

            return(await GetByIdAsync(dtoModel.Id));
        }
Exemplo n.º 26
0
        public async Task DeleteRole(string id)
        {
            var roleExists = await Context.GetRoleCollection().Find(x => x.Id.Equals(id)).AnyAsync();

            if (!roleExists)
            {
                CustomException.ThrowNotFoundException();
            }

            var deleteResult = await Context.GetRoleCollection().DeleteOneAsync(x => x.Id.Equals(id));

            if (!deleteResult.IsAcknowledged)
            {
                CustomException.ThrowBadRequestException("Delete failed.");
            }
        }
Exemplo n.º 27
0
        public override async Task <CityDto> Update(CityPutDto dtoModel)
        {
            if (!await ExistsAsync(dtoModel.Id))
            {
                CustomException.ThrowNotFoundException($"There is no city with id = {dtoModel.Id}.");
            }

            if (!await UnitOfWork.StateRepository.AnyAsync(x => x.Id == dtoModel.StateId))
            {
                CustomException.ThrowNotFoundException($"State with id = {dtoModel.StateId} doesn't exist.");
            }

            var cityDomain = ModelFactory.GetModel <City>(dtoModel);
            await UnitOfWork.CityRepository.Update(cityDomain);

            return(await GetByIdAsync(dtoModel.Id));
        }
Exemplo n.º 28
0
        public async Task <IHttpActionResult> PostClient(ClientPostDto client)
        {
            var user = await UserManager.FindByEmailAsync(client.Username);

            if (user == null)
            {
                CustomException.ThrowNotFoundException($"User: {client.Username} doesn't exist.");
            }

            var       messageToSend = "Username: "******"Please provide origin for JavaScript web application.");
                }

                if (client.AllowedOrigin.Equals("*"))
                {
                    CustomException.ThrowBadRequestException("Sorry we cannot allow unlimited origin. Please provide direct domain address.");
                }

                newClient = await ClientService.AddAsync(client);

                messageToSend += "<br>" + "client_id: " + newClient.Id;
            }
            else
            {
                var clientSecret = ClientService.GenerateClientSecret();

                client.ClientSecret  = clientSecret;
                client.AllowedOrigin = "*";

                newClient = await ClientService.AddAsync(client);

                messageToSend         += "<br>" + "client_id: " + newClient.Id + "<br>" + "client_secret: " + clientSecret;
                newClient.ClientSecret = clientSecret;
            }


            await UserManager.SendEmailAsync(user?.Id, "New client", $"{messageToSend}");

            return(CreatedAtRoute("ClientRoute", new { id = newClient.Id }, newClient));
        }
Exemplo n.º 29
0
        public async Task <IHttpActionResult> RemoveUserRole(UserRoleModel model)
        {
            var user = await UserManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                CustomException.ThrowNotFoundException($"There is no user {model.Email}.");
            }

            if (!await UserManager.IsInRoleAsync(user?.Id, model.RoleName))
            {
                CustomException.ThrowBadRequestException($"User {model.Email} is not in role {model.RoleName}.");
            }

            IdentityResult result = await UserManager.RemoveFromRoleAsync(user?.Id, model.RoleName);

            return(result.Succeeded ? Ok() : GetErrorResult(result));
        }
Exemplo n.º 30
0
        public async Task <PagedItems <ErrorLogDto> > GetAllByUserAsync(string userName, string skip, string take)
        {
            if (!await ExistsByUserAsync(userName))
            {
                CustomException.ThrowNotFoundException($"There are no logs asociated with user {userName}.");
            }

            var intSkip    = int.Parse(skip);
            var intTake    = int.Parse(take);
            var skipAmount = intTake * (intSkip - 1);

            var logs = await UnitOfWork.ErrorLogRepository.FindAllAsync(x => x.UserName.Equals(userName), x => x.OrderBy(y => y.ErrorDateTime), skipAmount, intTake);

            var logsDto   = logs.Select(ModelFactory.GetModel <ErrorLogDto>).ToList();
            var logsCount = await UnitOfWork.ErrorLogRepository.Count(x => x.UserName.Equals(userName));

            return(CreatePagedItems(logsDto, "UserErrorLogsRoute", intSkip, intTake, logsCount));
        }