Beispiel #1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUser = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                if (await userManager.IsInRoleAsync(currentUser, RoleNames.Inspector))
                {
                    throw new RestException(HttpStatusCode.Forbidden, new { Forbidden = "Permission Denied." });
                }

                var report = await context.Reports.FindAsync(request.Id);

                if (report == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Report = "Not found" });
                }

                context.Remove(report);

                var success = await context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem deleting report.");
            }
Beispiel #2
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUser = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                var doesTitleExist = CheckExistsByTitle(request.ReportType.Title);

                if (doesTitleExist)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Name = "Report Type title already exists." });
                }

                var newReportType = new ReportType
                {
                    Title         = request.ReportType.Title,
                    Fields        = request.ReportType.Fields,
                    RequireImages = request.ReportType.RequireImages,
                    IsActive      = true,
                    CreatedOn     = DateTime.UtcNow,
                    CreatedBy     = currentUser.FullName,
                    UpdatedBy     = "",
                    UpdatedOn     = null
                };

                context.ReportTypes.Add(newReportType);
                await context.SaveChangesAsync();

                return(Unit.Value);
            }
        public async Task <int> RemoveUserFromSiteAsync(AppUser user, long siteId)
        {
            if (user is null)
            {
                throw new System.ArgumentNullException(nameof(user));
            }

            var toRemove = await context.UserSites.FirstAsync((x => x.AppUserId == user.Id && x.SiteId == siteId));

            if (toRemove != null)
            {
                context.UserSites.Remove(toRemove);
                return(await context.SaveChangesAsync());
            }

            return(0);
        }
Beispiel #4
0
        public async Task <bool> AddAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            await entities.AddAsync(entity);

            bool succeeded = await context.SaveChangesAsync() > 0;

            if (!succeeded)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { Save = $"Problem adding {nameof(TEntity)}." });
            }
            return(succeeded);
        }
Beispiel #5
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUser = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                var isAdmin = await userManager.IsInRoleAsync(currentUser, RoleNames.Admin);

                var userCompanyId = currentUser.CompanyId;

                var image = await context.ReportImages.FirstOrDefaultAsync(x => x.Id == request.Id);

                if (image == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Image = "Not found" });
                }

                if (!isAdmin)
                {
                    var report = await context.Reports.FirstOrDefaultAsync(x => x.Id == image.ReportId);

                    if (report == null)
                    {
                        throw new RestException(HttpStatusCode.NotFound, new { Report = "Not found" });
                    }

                    var site = await context.Sites.FirstOrDefaultAsync(x => x.Id == report.SiteId);

                    if (site == null)
                    {
                        throw new RestException(HttpStatusCode.NotFound, new { Site = "Not found" });
                    }

                    var companyId = site.CompanyId;

                    if (userCompanyId != companyId)
                    {
                        throw new RestException(HttpStatusCode.Forbidden, new { Forbidden = "Permission Denied." });
                    }
                }

                //TODO: create Equals override in ReportImage class to compare objects
                image.FileName    = request.FileName;
                image.Description = request.Description;

                context.Entry(image).State = EntityState.Modified;

                var success = await context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception($"Problem updating {request.FileName}");
            }
Beispiel #6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUser = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                var isAdmin = await userManager.IsInRoleAsync(currentUser, RoleNames.Admin);

                var userCompanyId = currentUser.CompanyId;

                var image = await context.ReportImages.FindAsync(request.Id);

                if (image == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { site = "Not found" });
                }

                if (!isAdmin)
                {
                    var report = await context.Reports.FirstOrDefaultAsync(x => x.Id == image.ReportId);

                    if (report == null)
                    {
                        throw new RestException(HttpStatusCode.NotFound, new { Report = "Not found" });
                    }

                    var site = await context.Sites.FirstOrDefaultAsync(x => x.Id == report.SiteId);

                    if (site == null)
                    {
                        throw new RestException(HttpStatusCode.NotFound, new { Site = "Not found" });
                    }

                    var companyId = site.CompanyId;

                    if (userCompanyId != companyId)
                    {
                        throw new RestException(HttpStatusCode.Forbidden, new { Forbidden = "Permission Denied." });
                    }
                }

                context.Remove(image);
                var success = await context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Problem deleting image.");
            }
Beispiel #7
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await context.Users.FirstOrDefaultAsync(x => x.Id == userAccessor.GetCurrentUserId());

                if (user == null)
                {
                    return(Unit.Value);
                }

                if (request.Files.Count > 0)
                {
                    var images = new List <ReportImage>();

                    foreach (var image in request.Files)
                    {
                        using var ms = new MemoryStream();
                        image.CopyTo(ms);
                        var fileBytes = ms.ToArray();

                        images.Add(new ReportImage
                        {
                            ImageData = fileBytes,
                            FileName  = image.FileName,
                            FileType  = image.ContentType,
                            Size      = image.Length,
                            ReportId  = request.ReportId
                        });
                    }

                    await context.ReportImages.AddRangeAsync(images);

                    var succeeded = await context.SaveChangesAsync() > 0;

                    if (!succeeded)
                    {
                        throw new Exception("Error Saving Report Images.");
                    }

                    return(Unit.Value);
                }

                return(Unit.Value);
            }
Beispiel #8
0
            public async Task <long> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUser = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                var doesTitleExist = CheckExistsByTitle(request.Title);

                if (doesTitleExist)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Name = "Report title already exists." });
                }

                var newReport = new Report
                {
                    Title                = request.Title,
                    CompanyName          = request.CompanyName,
                    CompanyId            = request.CompanyId,
                    SiteId               = request.SiteId,
                    SiteName             = request.SiteName,
                    ReportTypeId         = request.ReportTypeId,
                    ReportType           = request.ReportType,
                    ReportFields         = request.ReportFields,
                    CompletionPercentage = request.CompletionPercentage,
                    RequireImages        = request.RequireImages,
                    IsActive             = true,
                    CreatedOn            = DateTime.UtcNow,
                    CreatedBy            = currentUser.FullName,
                    UpdatedBy            = "",
                    UpdatedOn            = null
                };

                context.Reports.Add(newReport);
                var succeeded = await context.SaveChangesAsync() > 0;

                if (!succeeded)
                {
                    throw new Exception("Error Saving New Report.");
                }

                return(await reportService.GetIdByTitleAsync(newReport.Title));
            }
Beispiel #9
0
            public async Task <ReportImage> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await context.Users.FirstOrDefaultAsync(x => x.Id == userAccessor.GetCurrentUserId());

                if (user == null)
                {
                    return(null);
                }

                var file = request.File;

                if (file.Length > 0)
                {
                    using var ms = new MemoryStream();
                    file.CopyTo(ms);
                    var fileBytes = ms.ToArray();

                    var newImage = new ReportImage
                    {
                        ImageData = fileBytes,
                        FileName  = file.FileName,
                        FileType  = file.ContentType,
                        Size      = file.Length,
                        ReportId  = 1
                    };

                    await context.ReportImages.AddAsync(newImage);

                    var succeeded = await context.SaveChangesAsync() > 0;

                    if (!succeeded)
                    {
                        throw new Exception("Error Saving Report Image.");
                    }

                    return(newImage);
                }

                return(null);
            }
Beispiel #10
0
 public async Task <bool> AddAsync(Company company)
 {
     context.Companies.Add(company);
     return(await context.SaveChangesAsync() > 0);
 }
Beispiel #11
0
 public async Task <bool> AddAsync(Site site)
 {
     context.Sites.Add(site);
     return(await context.SaveChangesAsync() > 0);
 }