예제 #1
0
        private JObject CreateOperationResponse(ApiActionResult apiActionResult, BulkOperationResult bulkOperation)
        {
            var result = new JObject();

            result[Common.Constants.BulkOperationRequestNames.Method]  = bulkOperation.Method.Method;
            result[Common.Constants.BulkOperationResponseNames.Status] = apiActionResult.StatusCode;
            if (!string.IsNullOrWhiteSpace(bulkOperation.BulkId))
            {
                result[Common.Constants.BulkOperationRequestNames.BulkId] = bulkOperation.BulkId;
            }

            if (!string.IsNullOrWhiteSpace(bulkOperation.Version))
            {
                result[Common.Constants.BulkOperationRequestNames.Version] = bulkOperation.Version;
            }

            if (!string.IsNullOrWhiteSpace(bulkOperation.Path))
            {
                result[Common.Constants.BulkOperationRequestNames.Path] = bulkOperation.Path;
            }

            if (!string.IsNullOrWhiteSpace(apiActionResult.Location))
            {
                result[Common.Constants.BulkOperationResponseNames.Location] = apiActionResult.Location;
            }

            if (apiActionResult.Content != null)
            {
                result.Add(new JProperty(Common.Constants.BulkOperationResponseNames.Response, JObject.FromObject(apiActionResult.Content)));
            }

            return(result);
        }
예제 #2
0
 public ApiActionResult Update(CommonResourcesUpdateModel model)
 {
     try
     {
         CommonResources obj = new CommonResources();
         obj = _dbConfigContext.CommonResources.AsNoTracking().FirstOrDefault(x => x.ResourceKey == model.ResourceKey);
         if (obj != null)
         {
             obj.ResourceValue  = model.ResourceValue;
             obj.TypeOfResource = model.TypeOfResource;
             _dbConfigContext.CommonResources.Update(obj);
             var flag = _dbConfigContext.SaveChanges();
             if (flag != 0)
             {
                 return(ApiActionResult.Success());
             }
             return(ApiActionResult.Failed("Updated failed"));
         }
         return(ApiActionResult.Failed("Can not updated because entity doest not existed!!"));
     }
     catch (Exception ex)
     {
         return(ApiActionResult.Failed(ex.Message));
     }
 }
예제 #3
0
        public virtual async Task <ApiActionResult> UpdateAsync(TUpdateModel model)
        {
            try
            {
                if (model == null)
                {
                    return(null);
                }

                if (!model.HasProperty("Id"))
                {
                    return(await ApiActionResult.FailedAsync("Data not found."));
                }
                var userid   = GetUserGuidId();
                var id       = model.GetValueByNameIfExists("Id");
                var entityDb = await _repository.FindEntityByIdAsync(id);

                if (entityDb == null)
                {
                    return(await ApiActionResult.FailedAsync("Data not found."));
                }
                var ignoreProperties = new LoopInjection(new[] { "CreatedBy", "CreatedDate", "UniqueId", "UniqueCode", "Id" });

                entityDb.InjectFrom(ignoreProperties, model);
                entityDb.SetValueByNameIfExists("LastModifiedBy", userid);
                entityDb.SetValueByNameIfExists("LastModifiedDate", DateTime.UtcNow);

                entityDb.InjectFrom(model);
                return(await _repository.UpdateEntityAsync(entityDb, userid));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #4
0
 public ApiActionResult CreateNewList(List <CommonResourcesCreateModel> model)
 {
     try
     {
         if (model.Count > 0)
         {
             for (int i = 0; i < model.Count; i++)
             {
                 CommonResources obj = new CommonResources();
                 obj = _mapper.Map <CommonResources>(model[i]);
                 if (_dbConfigContext.CommonResources.Any(x => x.ResourceKey == obj.ResourceKey && x.LanguageFid == obj.LanguageFid))
                 {
                     CommonResources updated = _dbConfigContext.CommonResources.Where(x => x.ResourceKey == obj.ResourceKey && x.LanguageFid == obj.LanguageFid).FirstOrDefault();
                     updated.ResourceValue  = obj.ResourceValue;
                     updated.TypeOfResource = obj.TypeOfResource;
                     _dbConfigContext.CommonResources.Update(updated);
                     _dbConfigContext.SaveChanges();
                 }
                 else
                 {
                     _dbConfigContext.CommonResources.Add(obj);
                     _dbConfigContext.SaveChanges();
                 }
             }
             return(ApiActionResult.Success());
         }
         return(ApiActionResult.Failed("Created Failed!!"));
     }
     catch (Exception ex)
     {
         return(ApiActionResult.Failed(ex.Message));
     }
 }
예제 #5
0
 public ApiActionResult Update(CommonLanguaguesUpdateModel model)
 {
     try
     {
         CommonLanguages obj = new CommonLanguages();
         obj = _dbConfigContext.CommonLanguages.AsNoTracking().FirstOrDefault(x => x.Id == model.Id);
         if (obj != null)
         {
             obj.LanguageCode = model.LanguageCode;
             obj.LanguageName = model.LanguageName;
             obj.Remarks      = model.Remarks;
             obj.ResourceKey  = model.ResourceKey;
             _dbConfigContext.CommonLanguages.Update(obj);
             int flag = _dbConfigContext.SaveChanges();
             if (flag != 0)
             {
                 return(ApiActionResult.Success());
             }
             return(ApiActionResult.Failed("Updated failed"));
         }
         return(ApiActionResult.Failed("Can not updated because entity doest not existed!!"));
     }
     catch (Exception ex)
     {
         return(ApiActionResult.Failed(ex.Message));
     }
 }
예제 #6
0
        public virtual async Task <ApiActionResult> DeleteEntityAsync(object id, object userId)
        {
            try
            {
                if (id == null)
                {
                    return(await ApiActionResult.FailedAsync("Entity can not be null."));
                }
                var entity = await FindEntityById(id);

                if (entity.HasProperty("Deleted", typeof(bool)))
                {
                    entity.TrySetPropertyValueByPropertyName("Deleted", true);
                    entity.TrySetPropertyValueByPropertyName("LastModifiedBy", userId);
                    entity.TrySetPropertyValueByPropertyName("LastModifiedDate", DateTime.Now);
                }
                else
                {
                    Entities.Remove(entity);
                    var result = await SaveChangesAsync();

                    if (result == 1)
                    {
                        return(await ApiActionResult.SuccessAsync());
                    }
                }

                return(await ApiActionResult.FailedAsync("Delete Entity Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #7
0
        public virtual async Task <ApiActionResult> CreateAsync(TCreateModel model)
        {
            try
            {
                if (model == null)
                {
                    return(null);
                }
                var     userId   = GetUserGuidId();
                string  uniqueId = UniqueIDHelper.GenarateRandomString(12);
                TEntity entity   = new TEntity();
                entity.InjectFrom(model);
                entity.SetValueByNameIfExists("CreatedBy", userId);
                entity.SetValueByNameIfExists("CreatedDate", DateTime.UtcNow);
                entity.SetValueByNameIfExists("LastModifiedBy", userId);
                entity.SetValueByNameIfExists("LastModifiedDate", DateTime.UtcNow);
                entity.SetValueByNameIfExists("Deleted", false);
                entity.SetValueByNameIfExists("UniqueId", uniqueId);

                var result = await _repository.CreateEntityAsync(entity, userId);

                var viewModel = new TViewModel();
                viewModel.InjectFrom(result.ResponseData);
                return(await ApiActionResult.SuccessAsync(viewModel));
            }
            catch (Exception ex)
            {
                return(await ApiActionResult.FailedAsync(ex.Message));
            }
        }
예제 #8
0
 public ApiActionResult UpdateCommonValues(CommonValueUpdateModel model)
 {
     try
     {
         CommonValues obj = new CommonValues();
         obj = _dbConfigContext.CommonValues.AsNoTracking().FirstOrDefault(x => x.Id == model.Id);
         if (obj != null)
         {
             obj.ValueDouble = model.ValueDouble;
             obj.ValueGroup  = model.ValueGroup;
             obj.ValueInt    = model.ValueInt;
             obj.ValueString = model.ValueString;
             obj.Text        = model.Text;
             obj.OrderBy     = model.OrderBy;
             var resUpdate = _dbConfigContext.CommonValues.Update(obj);
             int flag      = _dbConfigContext.SaveChanges();
             if (flag != 0)
             {
                 return(ApiActionResult.Success());
             }
             return(ApiActionResult.Failed("Updated failed"));
         }
         return(ApiActionResult.Failed("Can not updated because entity doest not existed!!"));
     }
     catch (Exception ex)
     {
         return(ApiActionResult.Failed(ex.Message));
     }
 }
예제 #9
0
        public virtual async Task <ApiActionResult> CreateEntityAsync(T entity, object userId)
        {
            try
            {
                if (entity == null)
                {
                    return(await ApiActionResult.FailedAsync("Entity can not be null."));
                }
                var trySetCreatedBy   = entity.TrySetPropertyValueByPropertyName("CreatedBy", userId);
                var trySetCreatedDate = entity.TrySetPropertyValueByPropertyName("CreatedDate", DateTime.Now);
                if (!trySetCreatedBy)
                {
                    //=>Log
                }
                if (!trySetCreatedDate)
                {
                    //=>Log
                }

                await Entities.AddAsync(entity);

                var result = await SaveChangesAsync();

                if (result == 1)
                {
                    return(await ApiActionResult.SuccessAsync());
                }

                return(await ApiActionResult.FailedAsync("Create Entity Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #10
0
        public virtual async Task <ApiActionResult> UpdateEntityAsync(T entity, object userId, LoopInjection ignoreProperties = null)
        {
            try
            {
                if (entity == null)
                {
                    return(await ApiActionResult.FailedAsync("Entity can not be null."));
                }

                var entityDb = await Entities.FindAsync(entity);

                if (ignoreProperties == null)
                {
                    ignoreProperties = new LoopInjection(new[] { "CreatedBy", "CreatedDate", "UniqueId", "Id" });
                }
                entityDb.InjectFrom(ignoreProperties, entity);
                entityDb.TrySetPropertyValueByPropertyName("LastModifiedBy", userId);
                entityDb.TrySetPropertyValueByPropertyName("LastModifiedDate", DateTime.Now);

                var result = await SaveChangesAsync();

                if (result == 1)
                {
                    return(await ApiActionResult.SuccessAsync());
                }

                return(await ApiActionResult.FailedAsync("Update Entities Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static ActionResult GetActionResult(this Controller controller, ApiActionResult result)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }


            if (!string.IsNullOrWhiteSpace(result.Location))
            {
                controller.HttpContext.Response.Headers[HeaderNames.Location] = result.Location;
            }

            if (result.Content != null)
            {
                var res = new ObjectResult(result.Content);
                res.StatusCode = result.StatusCode;
                return(res);
            }

            return(new StatusCodeResult(result.StatusCode.Value));
        }
예제 #12
0
        protected virtual IActionResult ResolveHttpResult(ApiActionResult apiActionResult)
        {
            IActionResult result = Ok("");

            if (!apiActionResult.Success)
            {
                if (apiActionResult.Errors.Any())
                {
                    var errors       = new ODataError();
                    var errorDetails = new List <ODataErrorDetail>();
                    foreach (var error in apiActionResult.Errors)
                    {
                        errorDetails.Add(new ODataErrorDetail
                        {
                            ErrorCode = error.Key,
                            Message   = error.Message,
                            Target    = error.Target
                        });
                    }
                    errors.Details = new ArraySegment <ODataErrorDetail>(errorDetails.ToArray());
                    var badResult = BadRequest(errors);
                    errors.ErrorCode = "" + badResult.StatusCode;
                    result           = badResult;
                }
                else
                {
                    result = NotFound();
                }
            }
            return(result);
        }
예제 #13
0
        public void TestCreatePayment_InvalidRequest()
        {
            SetUpGatewayComponent(false);
            var             result          = paymentController.CreatePayment(new PaymentDTO());
            var             objResult       = Assert.IsType <BadRequestObjectResult>(result);
            ApiActionResult apiActionResult = Assert.IsType <ApiActionResult>(objResult.Value);

            ((bool)apiActionResult.Data).Should().BeFalse();
        }
예제 #14
0
        public void TestCreatePayment_ReturnsTrue()
        {
            SetUpGatewayComponent(true);
            var             result          = paymentController.CreatePayment(new PaymentDTO());
            var             objResult       = Assert.IsType <OkObjectResult>(result);
            ApiActionResult apiActionResult = Assert.IsType <ApiActionResult>(objResult.Value);

            ((bool)apiActionResult.Data).Should().BeTrue();
        }
예제 #15
0
        public ApiActionResult UpdatePost(PostCreateModel model)
        {
            try
            {
                var obj = _dbCMSContext.Posts.AsNoTracking().FirstOrDefault(x => x.Id == model.Id);
                obj.DefaultTitle       = model.DefaultTitle;
                obj.TimeToRead         = model.TimeToRead;
                obj.IsActivated        = model.IsActivated;
                obj.Deleted            = model.Deleted;
                obj.CreatedBy          = obj.CreatedBy;
                obj.PostCategoryFid    = model.PostCategoryFid;
                obj.PostCategoryResKey = model.PostCategoryResKey;
                _dbCMSContext.Update(obj);
                _dbCMSContext.SaveChanges();

                var objDetail = _dbCMSContext.PostDetails.AsNoTracking().FirstOrDefault(x => x.Id == model.postDetail.Id);
                objDetail.KeyWord          = model.postDetail.KeyWord;
                objDetail.IsActivated      = model.postDetail.IsActivated;
                objDetail.LanguageFid      = model.postDetail.LanguageFid;
                objDetail.MetaDescription  = model.postDetail.MetaDescription;
                objDetail.Body             = model.postDetail.Body;
                objDetail.Deleted          = model.postDetail.Deleted;
                objDetail.Title            = model.postDetail.Title;
                objDetail.LastModifiedDate = DateTime.Now;
                objDetail.FileStreamFid    = model.postDetail.FileStreamFid;
                objDetail.FileTypeFid      = model.postDetail.FileTypeFid;

                var objFileStream = _dbCMSContext.PostFileStreams.AsNoTracking().Where(x => x.PostFid == model.Id).ToList();
                for (int i = 0; i < objFileStream.Count; i++)
                {
                    if (objFileStream[i].Id == model.postFileStream[i].Id)
                    {
                        objFileStream[i].ActivatedDate      = model.postFileStream[i].ActivatedDate;
                        objFileStream[i].FileCategoryFid    = model.postFileStream[i].FileCategoryFid;
                        objFileStream[i].FileCategoryResKey = model.postFileStream[i].FileCategoryResKey;
                        objFileStream[i].FileStreamFid      = model.postFileStream[i].FileStreamFid;
                        objFileStream[i].FileTypeFid        = model.postFileStream[i].FileTypeFid;
                        objFileStream[i].FileTypeResKey     = model.postFileStream[i].FileTypeResKey;
                        objFileStream[i].LastModifiedDate   = DateTime.Now;
                    }
                }
                return(ApiActionResult.Success());
            }
            catch (Exception ex)
            {
                return(ApiActionResult.Failed(ex.Message));
            }
        }
예제 #16
0
        public ApiActionResult Create(CommonLanguaguesCreateModel model)
        {
            try
            {
                CommonLanguages obj = new CommonLanguages();
                obj          = _mapper.Map <CommonLanguages>(model);
                obj.UniqueId = UniqueIDHelper.GenerateRandomString(12);

                _dbConfigContext.CommonLanguages.Add(obj);
                _dbConfigContext.SaveChanges();
                return(ApiActionResult.Success());
            }
            catch (Exception ex)
            {
                return(ApiActionResult.Failed(ex.Message));
            }
        }
예제 #17
0
        public ApiActionResult CreateNewPost(PostCreateModel model)
        {
            try
            {
                model.UniqueId = UniqueIDHelper.GenerateRandomString(12);
                Posts obj = new Posts();
                obj = _mapper.Map <Posts>(model);
                obj.LastModifiedDate = model.LastModifiedDate;
                obj.Deleted          = false;
                var res = _dbCMSContext.Posts.Add(obj);
                _dbCMSContext.SaveChanges();
                if (res.Entity.Id > 0)
                {
                    model.postFileStream.ForEach(x => x.PostFid = res.Entity.Id);
                    var objFileStream = model.postFileStream.Select(k => new PostFileStreams
                    {
                        FileCategoryFid    = k.FileCategoryFid,
                        FileCategoryResKey = k.FileCategoryResKey,
                        PostFid            = res.Entity.Id,
                        FileStreamFid      = k.FileStreamFid,
                        FileTypeFid        = k.FileTypeFid,
                        FileTypeResKey     = k.FileTypeResKey,
                        ActivatedDate      = k.ActivatedDate,
                        LastModifiedDate   = model.LastModifiedDate.Value,
                        LastModifiedBy     = k.LastModifiedBy,
                        Deleted            = false
                    });
                    _dbCMSContext.PostFileStreams.AddRange(objFileStream);
                    _dbCMSContext.SaveChanges();
                    PostDetails objDetail = new PostDetails();
                    objDetail = _mapper.Map <PostDetails>(model.postDetail);
                    objDetail.LastModifiedDate = model.postDetail.LastModifiedDate;
                    objDetail.Deleted          = false;
                    objDetail.PostFid          = res.Entity.Id;
                    var resDetail = _dbCMSContext.PostDetails.Add(objDetail);
                    _dbCMSContext.SaveChanges();
                }
                return(ApiActionResult.Success());
            }
            catch (Exception ex)
            {
                return(ApiActionResult.Failed(ex.Message));

                throw ex;
            }
        }
예제 #18
0
        public ApiActionResult CreateNewSubscriber(SubscriberCreateModel model)
        {
            try
            {
                Subscribers obj = new Subscribers();
                obj = _mapper.Map <Subscribers>(model);
                var res = _dbCMSContext.Subscribers.Add(obj);
                _dbCMSContext.SaveChanges();
                return(ApiActionResult.Success());
            }
            catch (Exception ex)
            {
                return(ApiActionResult.Failed(ex.Message));

                throw ex;
            }
        }
예제 #19
0
 public ApiActionResult Create(CommonResourcesCreateModel model)
 {
     try
     {
         CommonResources obj = new CommonResources();
         obj = _mapper.Map <CommonResources>(model);
         _dbConfigContext.CommonResources.Add(obj);
         var flag = _dbConfigContext.SaveChanges();
         if (flag != 0)
         {
             return(ApiActionResult.Success());
         }
         return(ApiActionResult.Failed("Created Failed!!"));
     }
     catch (Exception ex)
     {
         return(ApiActionResult.Failed(ex.Message));
     }
 }
예제 #20
0
 public ApiActionResult Delete(CommonResourcesCreateModel model)
 {
     try
     {
         var obj = _dbConfigContext.CommonResources.AsNoTracking().Where(x => x.ResourceKey == model.ResourceKey && x.LanguageFid == model.LanguageFid && x.TypeOfResource == model.TypeOfResource).FirstOrDefault();
         if (obj != null)
         {
             _dbConfigContext.CommonResources.Remove(obj);
             _dbConfigContext.SaveChanges();
             return(ApiActionResult.Success());
         }
         else
         {
             return(ApiActionResult.Failed("Can not find data to delete"));
         }
     }
     catch (Exception ex)
     {
         return(ApiActionResult.Failed(ex.Message));
     }
 }
예제 #21
0
        public ApiActionResult CreateNewCommonValues(CommonValueCreateModel model)
        {
            try
            {
                CommonValues obj = new CommonValues();
                obj          = _mapper.Map <CommonValueCreateModel, CommonValues>(model);
                obj.UniqueId = UniqueIDHelper.GenerateRandomString(12);
                _dbConfigContext.CommonValues.Add(obj);
                var flag = _dbConfigContext.SaveChanges();

                if (flag != 0)
                {
                    return(ApiActionResult.Success());
                }
                return(ApiActionResult.Failed("Created Failed"));
            }
            catch (Exception ex)
            {
                return(ApiActionResult.Failed(ex.Message));
            }
        }
예제 #22
0
        public virtual async Task <ApiActionResult> CreateRangeAsync(List <TCreateModel> models)
        {
            try
            {
                if (models == null)
                {
                    return(null);
                }
                var            userId   = GetUserGuidId();
                List <TEntity> entities = new List <TEntity>();
                foreach (var model in models)
                {
                    string  uniqueId = UniqueIDHelper.GenarateRandomString(12);
                    TEntity entity   = new TEntity();
                    entity.InjectFrom(model);
                    entity.SetValueByNameIfExists("CreatedBy", userId);
                    entity.SetValueByNameIfExists("CreatedDate", DateTime.UtcNow);
                    entity.SetValueByNameIfExists("LastModifiedBy", userId);
                    entity.SetValueByNameIfExists("LastModifiedDate", DateTime.UtcNow);
                    entity.SetValueByNameIfExists("Deleted", false);
                    entity.SetValueByNameIfExists("UniqueId", uniqueId);
                    entities.Add(entity);
                }

                var result = await _repository.CreateEntitiesAsync(entities, userId);

                if (result.Succeeded)
                {
                    return(await ApiActionResult.SuccessAsync());
                }
                return(await ApiActionResult.FailedAsync("Error"));
            }
            catch (Exception ex)
            {
                return(await ApiActionResult.FailedAsync(ex.Message));
            }
        }
예제 #23
0
        public virtual async Task <ApiActionResult> DeleteEntitiesAsync(List <T> entities, object userId)
        {
            try
            {
                if (entities == null)
                {
                    return(await ApiActionResult.FailedAsync("Entities can not be null."));
                }

                Entities.RemoveRange(entities);
                var result = await SaveChangesAsync();

                if (result == 1)
                {
                    return(await ApiActionResult.SuccessAsync());
                }

                return(await ApiActionResult.FailedAsync("Delete Entities Error."));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #24
0
        public async Task <ApiActionResult> Execute(JObject jObj, string baseUrl)
        {
            // 1. Check parameter.
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            // 2. Parse the request.
            var bulk = await _bulkRequestParser.Parse(jObj, baseUrl);

            if (!bulk.IsParsed)
            {
                return(_apiResponseFactory.CreateError(HttpStatusCode.InternalServerError,
                                                       bulk.ErrorResponse));
            }


            // 3. Execute bulk operation.
            var numberOfErrors   = 0;
            var operationsResult = new JArray();

            foreach (var operation in bulk.BulkResult.Operations)
            {
                ApiActionResult operationResult = null;
                if (operation.Method == HttpMethod.Post)
                {
                    operationResult = await _addRepresentationAction.Execute(operation.Data, operation.LocationPattern, operation.SchemaId, operation.ResourceType);
                }
                else if (operation.Method == HttpMethod.Put)
                {
                    operationResult = await _updateRepresentationAction.Execute(operation.ResourceId, operation.Data, operation.SchemaId, operation.LocationPattern, operation.ResourceType);
                }
                else if (operation.Method == HttpMethod.Delete)
                {
                    operationResult = await _deleteRepresentationAction.Execute(operation.ResourceId);
                }
                else if (operation.Method.Method == "PATCH")
                {
                    operationResult = await _patchRepresentationAction.Execute(operation.ResourceId, operation.Data, operation.SchemaId, operation.LocationPattern);
                }

                // 3.2. If maximum number of errors has been reached then return an error.
                if (!operationResult.IsSucceed())
                {
                    numberOfErrors++;
                    if (bulk.BulkResult.FailOnErrors.HasValue && numberOfErrors > bulk.BulkResult.FailOnErrors)
                    {
                        return(_apiResponseFactory.CreateError(HttpStatusCode.InternalServerError,
                                                               _errorResponseFactory.CreateError(
                                                                   string.Format(ErrorMessages.TheMaximumNumberOfErrorHasBeenReached, bulk.BulkResult.FailOnErrors),
                                                                   HttpStatusCode.InternalServerError,
                                                                   Common.Constants.ScimTypeValues.TooMany)));
                    }
                }

                operationsResult.Add(CreateOperationResponse(operationResult, operation));
            }

            var response = CreateResponse(operationsResult);

            return(_apiResponseFactory.CreateResultWithContent(HttpStatusCode.OK, response));
        }