Пример #1
0
        public async Task <bool> Delete(JObject data)
        {
            dynamic serviceData = data;
            int     id;

            try
            {
                id = serviceData.Id;
            }
            catch (Exception)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "Service Id"));
            }
            var service = await _contentManagementContext
                          .MasterDataKeyValues.SingleOrDefaultAsync(sr => sr.Id == id && sr.TypeId == (int)EntityIdentity.Service);

            if (service == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ServiceNotFound));
            }

            AuthorizeManager.SetAndCheckModifyAndAccessRole(service, null, false);


            if (service.EditMode)
            {
                _sourceControl.CheckCodeCheckOute(service);
            }

            var useCount = await _contentManagementContext.WebPages.Where(wp => wp.Services.Contains(service.PathOrUrl))
                           .CountAsync();

            if (useCount > 0)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.InUseItem, service.Name));
            }


            if (_fileSystemManager.FileExist(Path.Combine(Config.ServicesSourceCodePath, service.Guid + ".js")))
            {
                _sourceControl.RecycleBin(Config.ServicesSourceCodePath, service.Guid + ".js", codeNameIsFolder: false);

                DeleteFile(Config.ServicesSourceCodePath, service.Guid, ".js");
            }

            _contentManagementContext.MasterDataKeyValues.Remove(service);

            await _contentManagementContext.SaveChangesAsync();

            return(true);
        }