public Entity.BaseResponse <Guid> Manage([FromBody] Entity.AddLocationRequest request)
 {
     Entity.BaseResponse <Guid> response = new Entity.BaseResponse <Guid>(false);
     try
     {
         var status = _service.Manage(request);
         if (status.Success)
         {
             response.IsSuccess = status.Success;
             response.Message   = status.Message;
             response.Data      = status.Data;
         }
         else
         {
             response.IsSuccess = status.Success;
             response.Message   = status.Message;
         }
     }
     catch (Exception ex)
     {
         base.LogException(ex);
         return(new Entity.BaseResponse <Guid>(false, ex.Message));
     }
     return(response);
 }
예제 #2
0
        public Entity.ActionStatus Manage(Entity.AddLocationRequest request)
        {
            Entity.ActionStatus actionStatus = new Entity.ActionStatus(true);
            try
            {
                if (request.Guid == null || request.Guid == Guid.Empty)
                {
                    var checkExisting = _locationRepository.FindBy(x => x.Name.Equals(request.Name) && x.IsActive == true && !x.IsDeleted && x.CompanyGuid.Equals(SolutionConfiguration.CompanyId)).FirstOrDefault();
                    if (checkExisting == null)
                    {
                        var addEntityResult = AsyncHelpers.RunSync <IOT.DataResponse <IOT.AddEntityResult> >(() =>
                                                                                                             _iotConnectClient.Entity.Add(Mapper.Configuration.Mapper.Map <IOT.AddEntityModel>(request)));

                        if (addEntityResult != null && addEntityResult.status && addEntityResult.data != null)
                        {
                            request.Guid = Guid.Parse(addEntityResult.data.EntityGuid.ToUpper());
                            var dbLocation = Mapper.Configuration.Mapper.Map <Entity.AddLocationRequest, Model.Location>(request);
                            dbLocation.Guid        = request.Guid;
                            dbLocation.CompanyGuid = component.helper.SolutionConfiguration.CompanyId;
                            dbLocation.CreatedDate = DateTime.Now;
                            dbLocation.CreatedBy   = component.helper.SolutionConfiguration.CurrentUserId;
                            actionStatus           = _locationRepository.Manage(dbLocation);
                            actionStatus.Data      = (Guid)(actionStatus.Data);
                            if (!actionStatus.Success)
                            {
                                _logger.ErrorLog(new Exception($"Location is not added in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                                var deleteEntityResult = _iotConnectClient.Entity.Delete(request.Guid.ToString()).Result;
                                if (deleteEntityResult != null && deleteEntityResult.status)
                                {
                                    _logger.ErrorLog(new Exception($"Location is not deleted from iotconnect, Error: {deleteEntityResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                                    actionStatus.Success = false;
                                    actionStatus.Message = new UtilityHelper().IOTResultMessage(deleteEntityResult.errorMessages);
                                }
                            }
                        }
                        else
                        {
                            _logger.ErrorLog(new Exception($"Location is not added in iotconnect, Error: {addEntityResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                            actionStatus.Success = false;
                            actionStatus.Message = new UtilityHelper().IOTResultMessage(addEntityResult.errorMessages);
                        }
                    }
                    else
                    {
                        _logger.InfoLog($"Location Already Exist !!");
                        actionStatus.Success = false;
                        actionStatus.Message = "Location Name Already Exists";
                    }
                }
                else
                {
                    var olddbLocation = _locationRepository.FindBy(x => x.Guid.Equals(request.Guid)).FirstOrDefault();
                    if (olddbLocation == null)
                    {
                        throw new NotFoundCustomException($"{CommonException.Name.NoRecordsFound} : Location");
                    }

                    var updateEntityResult = _iotConnectClient.Entity.Update(request.Guid.ToString(), Mapper.Configuration.Mapper.Map <IOT.UpdateEntityModel>(request)).Result;
                    if (updateEntityResult != null && updateEntityResult.status && updateEntityResult.data != null)
                    {
                        var dbLocation = Mapper.Configuration.Mapper.Map(request, olddbLocation);
                        dbLocation.UpdatedDate = DateTime.Now;
                        dbLocation.UpdatedBy   = component.helper.SolutionConfiguration.CurrentUserId;
                        dbLocation.CompanyGuid = component.helper.SolutionConfiguration.CompanyId;
                        actionStatus           = _locationRepository.Manage(dbLocation);
                        actionStatus.Data      = (Guid)actionStatus.Data;
                        if (!actionStatus.Success)
                        {
                            _logger.ErrorLog(new Exception($"Location is not updated in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                            actionStatus.Success = false;
                            actionStatus.Message = "Something Went Wrong!";
                        }
                    }
                    else
                    {
                        _logger.ErrorLog(new Exception($"Location is not added in iotconnect, Error: {updateEntityResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                        actionStatus.Success = false;
                        actionStatus.Message = new UtilityHelper().IOTResultMessage(updateEntityResult.errorMessages);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorLog(ex, "LocationService.Manage " + ex);
                actionStatus.Success = false;
                actionStatus.Message = ex.Message;
            }
            return(actionStatus);
        }