コード例 #1
0
        public Task <NotiInfo> GetNotificationList(int userID, int currentIndex, int maxRow)
        {
            return(Task.Run(() =>
            {
                using (MIUEntities db = new MIUEntities())
                {
                    NotiInfo Notifications = new NotiInfo();

                    try
                    {
                        User user = db.Users.Where(x => x.ID == userID && x.IsDelete != true).SingleOrDefault();
                        Notifications.NotiCount = 0;
                        var skip = (currentIndex - 1) * maxRow;

                        List <Notification> NotiList = (from n in db.Notifications
                                                        where n.NotiTypeId != 1 && (!db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID) || db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID))
                                                        select n).ToList();

                        List <Notification> SubscriptionList = (from n in db.Notifications
                                                                //join nv in db.NotiVisibilities.Where(nv => nv.UserID != userID) on n.ID equals nv.NotiID into noti from x in noti.DefaultIfEmpty()
                                                                join s in db.Subscribtions.Where(s => s.UserID == userID) on n.ID equals s.NotiID
                                                                where (!db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID) || db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID)) && n.NotiTypeId == 1
                                                                select n).ToList();

                        List <NotiVisibility> notiVisibilities = new List <NotiVisibility>();
                        List <NotiListInfo> NotiListInfo = new List <NotiListInfo>();

                        foreach (var data in NotiList)
                        {
                            NotiListInfo info = new NotiListInfo();
                            PropertyCopier <Notification, NotiListInfo> .Copy(data, info);

                            var NotiType = db.NotificationTypes.Where(x => x.ID == data.NotiTypeId).SingleOrDefault();

                            if (NotiType.ID == 1)
                            {
                                LeadNotification LeadNoti = new LeadNotification();
                                LeadNoti = db.LeadNotifications.Where(x => x.NotificationId == data.ID).FirstOrDefault();
                                if (LeadNoti != null)
                                {
                                    info.RelatedTableID = LeadNoti.LeadId;
                                    info.RelatedTableName = "Lead";
                                }
                            }
                            else if (NotiType.ID == 2)
                            {
                                NewsNoti NewNoti = new NewsNoti();
                                NewNoti = db.NewsNotis.Where(x => x.NotiID == data.ID).FirstOrDefault();
                                if (NewNoti != null)
                                {
                                    info.RelatedTableID = NewNoti.NewsID;
                                    info.RelatedTableName = "News";
                                }
                            }

                            //info.RelatedTableID = db.
                            NotiListInfo.Add(info); //Get Notification List

                            NotiVisibility notiVisibility = new NotiVisibility();
                            notiVisibilities = db.NotiVisibilities.Where(x => x.UserID == userID && x.NotiID == data.ID).ToList();
                            if (notiVisibilities.Count == 0)
                            {
                                notiVisibility.NotiID = data.ID;
                                notiVisibility.UserID = userID;
                                notiVisibility.IsSeen = false;
                                db.NotiVisibilities.Add(notiVisibility);
                            }
                            else
                            {
                                foreach (var notiVis in notiVisibilities)
                                {
                                    foreach (var NotiListing in NotiListInfo)
                                    {
                                        if (NotiListing.UserId != null && NotiListing.ID == notiVis.NotiID && NotiListing.UserId == notiVis.UserID && notiVis.IsSeen == true)
                                        {
                                            NotiListing.IsSeen = true;
                                        }
                                        else if (NotiListing.ID == notiVis.NotiID && notiVis.IsSeen == true)
                                        {
                                            NotiListing.IsSeen = true;
                                        }
                                    }
                                }
                            }
                        }

                        foreach (var data in SubscriptionList)
                        {
                            NotiListInfo info = new NotiListInfo();
                            PropertyCopier <Notification, NotiListInfo> .Copy(data, info);

                            var NotiType = db.NotificationTypes.Where(x => x.ID == data.NotiTypeId).SingleOrDefault();

                            if (NotiType.ID == 1)
                            {
                                LeadNotification LeadNoti = db.LeadNotifications.Where(x => x.NotificationId == data.ID).FirstOrDefault();
                                if (LeadNoti != null)
                                {
                                    info.RelatedTableID = LeadNoti.LeadId;
                                    info.RelatedTableName = "Lead";
                                }
                            }
                            else if (NotiType.ID == 2)
                            {
                                NewsNoti NewNoti = db.NewsNotis.Where(x => x.NotiID == data.ID).FirstOrDefault();
                                if (NewNoti != null)
                                {
                                    info.RelatedTableID = NewNoti.NewsID;
                                    info.RelatedTableName = "News";
                                }
                            }

                            //info.RelatedTableID = db.
                            NotiListInfo.Add(info); //Get Notification List

                            NotiVisibility notiVisibility = new NotiVisibility();
                            notiVisibilities = db.NotiVisibilities.Where(x => x.UserID == userID && x.NotiID == data.ID).ToList();
                            if (notiVisibilities.Count == 0)
                            {
                                notiVisibility.NotiID = data.ID;
                                notiVisibility.UserID = userID;
                                notiVisibility.IsSeen = false;
                                db.NotiVisibilities.Add(notiVisibility);
                            }
                            else
                            {
                                foreach (var notiVis in notiVisibilities)
                                {
                                    foreach (var NotiListing in NotiListInfo)
                                    {
                                        if (NotiListing.ID == notiVis.NotiID && NotiListing.UserId == notiVis.UserID && notiVis.IsSeen == true)
                                        {
                                            NotiListing.IsSeen = true;
                                            //NotiListInfo.Add(NotiListing);
                                        }
                                    }
                                }
                            }
                        }

                        List <NotiListInfo> WebNotiList = new List <NotiListInfo>();
                        WebNotiList = GetWebNotification(userID);

                        NotiListInfo.AddRange(WebNotiList);

                        NotiListInfo.ForEach(x => { if (x.UpdatedDate == null)
                                                    {
                                                        x.UpdatedDate = x.CreatedDate; x.UpdatedBy = x.CreatedBy;
                                                    }
                                             });                                                                                                // if updated date is null set updated date value from created date.

                        Notifications.NotiList = NotiListInfo.OrderByDescending(x => x.CreatedDate).Skip(skip).Take(maxRow).ToList();
                        db.SaveChanges(); //Add to IsSeenNotification Table

                        int TotalNotiCount = db.Notifications.Where(x => x.NotiTypeId != 1).Count();
                        int IsSeenNotiCount = db.NotiVisibilities.Where(x => x.UserID == userID).Count();

                        Notifications.NotiCount = 0;
                    }
                    catch (System.Exception ex)
                    {
                        //throw ex;
                    }

                    return Notifications;
                }
            }));
        }
コード例 #2
0
        public async Task <HttpResponseMessage> PasswordChange(ChangePassword req)
        {
            try
            {
                SettingDAL dal = new SettingDAL();
                //JsonResponse response = await PasswordChanges(req);
                JsonResponse response = new JsonResponse();
                using (MIUEntities db = new MIUEntities())
                {
                    try
                    {
                        User user = db.Users.Where(x => x.ID == req.UserID).FirstOrDefault();
                        if (user == null)
                        {
                            response.Flag    = true;
                            response.Message = "User not found.";
                        }
                        else
                        {
                            string Message = "";
                            if (user.Password == req.CurrentPassword)
                            {
                                if (req.NewPassword == req.ComfirmPassword)
                                {
                                    user.Password = req.NewPassword;
                                    var            AspNetUserID = db.AspNetUsers.Where(x => x.UserName == user.LoginName).Select(x => x.Id).FirstOrDefault();
                                    IdentityResult result       = await UserManager.ChangePasswordAsync(AspNetUserID, req.CurrentPassword, req.NewPassword);

                                    if (!result.Succeeded)
                                    {
                                        Message = "Fail to change password!";
                                    }
                                    else
                                    {
                                        db.SaveChanges();
                                        Message = "Update Password Successfully!.";
                                    }
                                }
                                else
                                {
                                    Message = "New password and confirm password are not same.";
                                }
                            }
                            else
                            {
                                Message = "Current Password is incorrect.";
                            }
                            //db.SaveChanges();
                            response.Flag    = true;
                            response.Message = Message;
                        }
                    }
                    catch (Exception ex)
                    {
                        //return new JsonResponse() { Flag = true, Message = Message };
                        response.Flag    = true;
                        response.Message = ex.Message;
                    }
                }
                if (response != null && response.Flag)
                //if (response)
                {
                    return(Request.CreateResponse <JsonResponse>(HttpStatusCode.OK, response));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, MIUWebAPI.Helper.Constants.ErrorNotFound));
                }
            }
            catch (DbEntityValidationException ex)
            {
                var    controllerName = ControllerContext.RouteData.Values["controller"].ToString();
                var    actionName     = ControllerContext.RouteData.Values["action"].ToString();
                Logger log            = new Logger();
                log.ErrorLog(ex, controllerName, actionName);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, MIUWebAPI.Helper.Constants.ErrorSysError));
            }
        }
コード例 #3
0
        public Task <JsonResponse> ForgotPassword(string email)
        {
            return(Task.Run(() =>
            {
                using (MIUEntities db = new MIUEntities())
                {
                    try
                    {
                        User user = db.Users.Where(x => x.EmailAccount == email).FirstOrDefault();
                        if (user == null)
                        {
                            return new JsonResponse()
                            {
                                Flag = true, Message = "User is not found"
                            };;
                        }
                        else
                        {
                            Random rnd = new Random();
                            string code = rnd.Next(1, 999999).ToString("D6");
                            AspNetUser aspNetUser = db.AspNetUsers.Where(x => x.Email == email).FirstOrDefault();
                            var check = (from a in db.AspNetUserAuthentications where a.UserID == aspNetUser.Id select a.UserID).FirstOrDefault();
                            AspNetUserAuthentication auth = new AspNetUserAuthentication();
                            if (check == null)
                            {
                                auth = new AspNetUserAuthentication()
                                {
                                    UserID = aspNetUser.Id,
                                    Code = code,
                                    CreatedDate = DateTime.Now
                                };
                                db.AspNetUserAuthentications.Add(auth);
                                db.SaveChanges();
                            }
                            else
                            {
                                auth = new AspNetUserAuthentication();
                                auth = db.AspNetUserAuthentications.Where(a => a.UserID == aspNetUser.Id).FirstOrDefault();
                                auth.Code = code;
                                auth.CreatedDate = DateTime.Now;
                                db.SaveChanges();
                            }
                            code = "Your two factor authentication code is below \n" + code;
                            EmailManager.SendEmail(code, email, "Two Factor Authentication Code");

                            //db.SaveChanges();
                            return new JsonResponse()
                            {
                                Flag = true, Message = "Successfully Sent", ReferenceKey = auth.UserID
                            };
                        }
                    }
                    catch (Exception ex)
                    {
                        return new JsonResponse()
                        {
                            Flag = false, Message = ex.Message
                        };
                    }
                }
            }));
        }
コード例 #4
0
        //public Task<List<string>> GetMIUResourceKeyword()
        //{
        //    return Task.Run(() =>
        //    {
        //        using (MIUEntities db = new MIUEntities())
        //        {
        //            try
        //            {
        //                List<string> infoList = new List<string>();

        //                var KeywordList = db.MICResourceFiles.GroupBy(x => x.Keyword).Select(x => x.Key == null ? "Other" : x.Key).ToList();

        //                return KeywordList;
        //            }
        //            catch (Exception)
        //            {
        //                return null;
        //            }

        //        }
        //    });
        //}

        public Task <List <MIUResourceInfo> > GetMIUResource(string Sorting, string Name)
        {
            return(Task.Run(() =>
            {
                using (MIUEntities db = new MIUEntities())
                {
                    List <MIUResourceInfo> infoList = new List <MIUResourceInfo>();
                    List <MICResourceFile> dataList = db.MICResourceFiles.ToList();
                    try
                    {
                        if (string.IsNullOrEmpty(Sorting) || Sorting == "\"\"")
                        {
                            dataList = dataList.OrderByDescending(x => x.CreatedDate != null ? x.CreatedDate : x.ModifiedDate).OrderByDescending(x => x.IsFolder).ToList();

                            if (!String.IsNullOrEmpty(Name) && Name != "\"\"")
                            {
                                dataList = dataList.Where(x => x.Name.Contains(Name)).ToList();
                            }
                        }
                        else if (Sorting == "ascending")
                        {
                            dataList = dataList.OrderBy(x => x.Name).OrderByDescending(x => x.IsFolder).ToList();

                            if (!String.IsNullOrEmpty(Name) && Name != "\"\"")
                            {
                                dataList = dataList.Where(x => x.Name.Contains(Name)).ToList();
                            }
                        }
                        else if (Sorting == "descending")
                        {
                            dataList = dataList.OrderByDescending(x => x.Name).OrderByDescending(x => x.IsFolder).ToList();

                            if (!String.IsNullOrEmpty(Name) && Name != "\"\"")
                            {
                                dataList = dataList.Where(x => x.Name.Contains(Name)).ToList();
                            }
                        }
                        //if(!String.IsNullOrEmpty(Name) && Name != "\"\"")
                        //{
                        //    dataList = dataList.Where(x => x.Name.Contains(Name)).ToList();
                        //}

                        foreach (var data in dataList)
                        {
                            MIUResourceInfo info = new MIUResourceInfo();

                            PropertyCopier <MICResourceFile, MIUResourceInfo> .Copy(data, info);
                            //info.FilePath = MIUFileServer.GetFileUrl("Resources/MIUResource", data.Name);
                            info.FilePath = MIUFileServer.GetFileUrl("Resource/MICResources", data.Name);
                            if (data.IsFolder == 1)
                            {
                                info.FileCount = db.MIUResourceFolderDetails.Where(x => x.MIUResourceID == data.ID).Count();
                            }
                            infoList.Add(info);
                        }

                        return infoList;
                    }
                    catch (Exception)
                    {
                        return infoList;
                    }
                }
            }));
        }