Пример #1
0
        public async Task <Unit> UpdateByIdAsync(Command request)
        {
            var siteToUpdate = await context.Sites.FindAsync(request.Id);

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

            var requestSite = new Site(request.Name, request.Address, request.Notes);

            if (siteToUpdate.Equals(requestSite))
            {
                throw new RestException(HttpStatusCode.BadRequest, new { Data = "No Changes Found" });
            }

            siteToUpdate.Name    = request.Name;
            siteToUpdate.Address = request.Address;
            siteToUpdate.Notes   = request.Notes;

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

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

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

            throw new Exception($"Problem updating {request.Name}");
        }
Пример #2
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}");
            }
Пример #3
0
 public void Update(TEntity entity)
 {
     entities.Attach(entity);
     context.Entry(entity).State = EntityState.Modified;
 }