Exemplo n.º 1
0
        public async Task <T> Update(int id, T entity)
        {
            using (RatDbContext context = _contextFactory.CreateDbContext())
            {
                entity.Id = id;
                context.Set <T>().Update(entity);
                await context.SaveChangesAsync();

                return(entity);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> Delete(int id)
        {
            using (RatDbContext context = _contextFactory.CreateDbContext())
            {
                T entity = await context.Set <T>().FirstOrDefaultAsync((e) => e.Id == id);

                context.Set <T>().Remove(entity);
                await context.SaveChangesAsync();

                return(true);
            }
        }
Exemplo n.º 3
0
        public async Task <GetProjectsForUserResponse> Handle(GetProjectsForUserRequest request, CancellationToken cancellationToken)
        {
            request.Validate();

            if (request.Context.Status != ProcessingStatus.GoodRequest)
            {
                return new() { Context = request.Context }
            }
            ;

            var userId = request.UserId;
            var user   =
                await
                _context.Users
                .Include(x => x.Projects)
                .ThenInclude(x => x.Project)
                .FirstOrDefaultAsync(x => x.UserId == userId, cancellationToken);

            if (user == null)
            {
                var userEntity = await _context.Users.AddAsync(new() { UserId = request.UserId }, cancellationToken);

                var expectedNumberOfChanges = 1;
                var changes = await _context.SaveChangesAsync(cancellationToken);

                if (changes != expectedNumberOfChanges)
                {
                    throw new RatDbException(string.Format(Resources.ExpactedAndActualNumberOfDatabaseChangesMismatch, changes, expectedNumberOfChanges));
                }

                user = userEntity.Entity;
            }

            request.Context.Status = ProcessingStatus.Ok;

            return(new()
            {
                Context = request.Context,
                UserProjectStats = new()
                {
                    UserId = user.Id,
                    ProjectStats = user.Projects.Select(x => new ProjectStatsView
                    {
                        Id = x.ProjectId,
                        Name = x.Project.Name,
                        TotalConfigurationCount = 0,
                        TotalEntryCount = 0
                    })
                }
            });
        }
    }
Exemplo n.º 4
0
        public async Task <CreateProjectResponse> Handle(CreateProjectRequest request, CancellationToken cancellationToken)
        {
            var projectTypeId = request.ProjectTypeId;
            var userId        = request.UserId;

            var projectType = await _context.ProjectTypes.FirstOrDefaultAsync(x => x.Id == projectTypeId, cancellationToken);

            var user = await _context.Users.Where(x => x.UserId == userId).FirstOrDefaultAsync(cancellationToken);

            request.Validate(projectType, user);

            if (request.Context.Status != ProcessingStatus.GoodRequest)
            {
                return new() { Context = request.Context }
            }
            ;

            var project =
                await
                _context.Projects.AddAsync(
                    new ProjectEntity { Name = request.Name, Type = projectType },
                    cancellationToken);

            await _context.ProjectUsers.AddAsync(
                new ProjectUserEntity { Project = project.Entity, User = user },
                cancellationToken);

            var expectedNumberOfChanges = 2;
            var changes = await _context.SaveChangesAsync(cancellationToken);

            if (changes != expectedNumberOfChanges)
            {
                throw new RatDbException(
                          string.Format(
                              Resources.ExpactedAndActualNumberOfDatabaseChangesMismatch,
                              changes,
                              expectedNumberOfChanges));
            }

            request.Context.Status = ProcessingStatus.Ok;

            return(new()
            {
                Project = new() { Id = project.Entity.Id, Name = project.Entity.Name, TypeId = project.Entity.Type.Id },
                Context = request.Context
            });
        }
    }
Exemplo n.º 5
0
        public async Task <PatchProjectResponse> Handle(PatchProjectRequest request, CancellationToken cancellationToken)
        {
            var projectType = await _context.ProjectTypes.FirstOrDefaultAsync(x => x.Id == request.ProjectTypeId, cancellationToken);

            request.Validate(projectType);

            if (request.Context.Status != ProcessingStatus.GoodRequest)
            {
                return new() { Context = request.Context }
            }
            ;

            var project = await _context.Projects.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (project == null)
            {
                request.Context.Status = ProcessingStatus.NotFound;

                return(new() { Context = request.Context });
            }

            project.Name = request.Name;
            project.Type = projectType;

            _context.Projects.Update(project);

            var expectedNumberOfChanges = 1;
            var changes = await _context.SaveChangesAsync(cancellationToken);

            if (changes != expectedNumberOfChanges)
            {
                throw new RatDbException(string.Format(Resources.ExpactedAndActualNumberOfDatabaseChangesMismatch, changes, expectedNumberOfChanges));
            }

            request.Context.Status = ProcessingStatus.Ok;

            return(new()
            {
                Context = request.Context,
                Project = new() { Id = project.Id, Name = project.Name, TypeId = project.Type.Id }
            });
        }
    }
Exemplo n.º 6
0
        public async Task <DeleteProjectResponse> Handle(
            [NotNull] DeleteProjectRequest request,
            CancellationToken cancellationToken)
        {
            request.Validate();

            if (request.Context.Status != ProcessingStatus.GoodRequest)
            {
                return new() { Context = request.Context }
            }
            ;

            var projectId = request.Id;
            var project   = await _context.Projects.FirstOrDefaultAsync(x => x.Id == projectId, cancellationToken);

            if (project == null)
            {
                request.Context.Status = ProcessingStatus.NotFound;

                return(new() { Context = request.Context });
            }

            _context.Projects.Remove(project);

            var expectedNumberOfChanges = 1;
            var changes = await _context.SaveChangesAsync(cancellationToken);

            if (changes != expectedNumberOfChanges)
            {
                throw new RatDbException(string.Format(Resources.ExpactedAndActualNumberOfDatabaseChangesMismatch, changes, expectedNumberOfChanges));
            }

            request.Context.Status = ProcessingStatus.Ok;

            return(new DeleteProjectResponse {
                Context = request.Context
            });
        }
    }