public AttachmentLog Insert(int simCardId, int assetId, int appId, int userId, string username, int addOrRemove = 1)
        {
            var entity = new AttachmentLog
            {
                SimCardId = simCardId,
                AssetId   = assetId,
                AppId     = appId,
                UserName  = username,
                LogType   = addOrRemove,
                UserId    = userId
            };

            _context.AttachmentLogs.Add(entity);
            return(entity);
        }
        public AttachmentLog Insert(Attachment attachment, int addOrRemove = 1)
        {
            var entity = new AttachmentLog
            {
                SimCardId = attachment.SimCardId,
                AssetId   = attachment.AssetId,
                AppId     = attachment.AppId,
                LogType   = addOrRemove,
                UserName  = attachment.UserName,
                UserId    = attachment.UserId
            };

            _context.AttachmentLogs.Add(entity);
            return(entity);
        }
        public async Task <AttachmentLog> InsertAsync(Attachment attachment, int addOrRemove = 1)
        {
            var entity = new AttachmentLog
            {
                SimCardId = attachment.SimCardId,
                AssetId   = attachment.AssetId,
                AppId     = attachment.AppId,
                LogType   = addOrRemove,
                UserName  = attachment.UserName,
                UserId    = attachment.UserId
            };
            await _context.AttachmentLogs.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(await Task.FromResult(entity));
        }
        public ActionResult AddUserAttachment(NueRequestAttchment nueRequestAttchment)
        {
            string retrunResponse = "";
            string requestId      = nueRequestAttchment.requestId;

            try
            {
                UserProfile currentUser = (Session["UserProfileSession"] as UserProfile);
                if (currentUser == null || requestId == null ||
                    requestId.Trim() == "")
                {
                    throw new Exception("Invalid request");
                }
                NueRequestActivityMaster nueRequestActivityMaster = new DataAccess().getRequestActivityMasterId("File");
                UserRequest userRequest = new DataAccess().getRequestDetailsByReqId(requestId);
                if (userRequest == null)
                {
                    throw new Exception("Invalid request");
                }

                var  userAceess = currentUser.userAccess;
                bool userAccess = false;

                int adminUsers = userAceess.Where(x => (x.AccessDesc == "Root_Admin" || x.AccessDesc == "Hcm_Admin" || x.AccessDesc == "Hcm_User")).Count();
                List <NueRequestAceessLog> nueRequestAceessLogs     = new DataAccess().getRequestAccessList(requestId);
                List <UserProfile>         userProfiles             = new DataAccess().getAllUserProfiles();
                NeuLeaveCancelationModal   neuLeaveCancelationModal = new DataAccess().getNeuLeaveCancelationDetails(requestId);
                if (userRequest == null)
                {
                    throw new Exception("Invalid request");
                }

                if (adminUsers > 0)
                {
                    userAccess = true;
                }

                if (nueRequestAceessLogs.Where(x => (x.UserId == currentUser.Id && x.OwnerId != currentUser.Id)).Count() > 0)
                {
                    userAccess = true;
                }

                if (userRequest.OwnerId == currentUser.Id)
                {
                    userAccess = true;
                }

                if (userAccess)
                {
                    string FileName      = Path.GetFileNameWithoutExtension(nueRequestAttchment.requestAtchmentFile.FileName);
                    string FileExtension = Path.GetExtension(nueRequestAttchment.requestAtchmentFile.FileName);
                    string VFileName     = DateTime.UtcNow.ToString("yyyyMMddhhmmss") + "_" + FileExtension;

                    var           dateCreated   = DateTime.UtcNow;
                    AttachmentLog attachmentLog = new AttachmentLog();
                    attachmentLog.RequestId  = userRequest.NueRequestMasterId;
                    attachmentLog.Request    = userRequest.RequestId;
                    attachmentLog.UserId     = currentUser.Id;
                    attachmentLog.OwnerId    = userRequest.OwnerId;
                    attachmentLog.FileName   = FileName;
                    attachmentLog.FileExt    = FileExtension;
                    attachmentLog.VFileName  = VFileName;
                    attachmentLog.AddedOn    = dateCreated;
                    attachmentLog.ModifiedOn = dateCreated;

                    string UploadPath = ConfigurationManager.AppSettings["UserFilePath"].ToString();
                    //var tempUPath = Server.MapPath(UploadPath + requestId).Replace(@"\", "/");
                    var tempUPath = (UploadPath + requestId).Replace(@"\", "/");

                    bool exists = System.IO.Directory.Exists(tempUPath);
                    if (!exists)
                    {
                        System.IO.Directory.CreateDirectory(tempUPath);
                    }

                    string vFilePath = UploadPath + requestId + "/" + VFileName;
                    nueRequestAttchment.requestAtchmentFile.SaveAs(vFilePath);

                    int addedId = new DataAccess().addRequestAttachmentLog(attachmentLog);
                    if (addedId != -1)
                    {
                        NueRequestActivity nueRequestActivity = new NueRequestActivity();
                        nueRequestActivity.Payload     = VFileName;
                        nueRequestActivity.PayloadType = nueRequestActivityMaster.Id;
                        nueRequestActivity.UserId      = currentUser.Id;
                        nueRequestActivity.RequestId   = userRequest.NueRequestMasterId;
                        nueRequestActivity.Request     = userRequest.RequestId;
                        nueRequestActivity.AddedOn     = dateCreated;
                        nueRequestActivity.ModifiedOn  = dateCreated;
                        new DataAccess().addRequestComment(nueRequestActivity);
                        retrunResponse = "File added successfully.";
                    }
                    else
                    {
                        throw new Exception("Invalid request");
                    }
                }
                else
                {
                    retrunResponse = "Invalid request";
                }
            }
            catch (Exception e)
            {
                retrunResponse = "An error occerd.";
            }
            finally
            {
            }
            return(RedirectToAction("SelfRequestDetails", new { requestId = requestId, message = retrunResponse }));
        }