浏览代码

реализация notification

ganahrhr 3 年之前
父节点
当前提交
961e293105
共有 6 个文件被更改,包括 241 次插入30 次删除
  1. 30 24
      Models/Notification.cs
  2. 1 1
      MySQLConnector.cs
  3. 141 0
      Pages/ModalNotifications.razor
  4. 12 2
      Shared/Sidebar.razor
  5. 47 3
      Shared/Sidebar.razor.cs
  6. 10 0
      wwwroot/css/style.css

+ 30 - 24
Models/Notification.cs

@@ -10,36 +10,34 @@ namespace HyperCube.Models
     public class Notification
     {
         public int ID { get; set; }
-        public NotificationType Type { get; set; }
+        public NotificationType NotifiType { get; set; }
         public string Header { get; set; }
         public string Body { get; set; }
-        public bool IsRead
-        {
-            get { return IsRead; }
-            set { DateRead = IsRead ? DateTime.Now : null; }
-        }
-        public DateTime? DateRead { get; private set; }
-        public bool IsApproved
-        {
-            get { return IsApproved; }
-            set { DateApprove = IsApproved ? DateTime.Now : null; }
-        }
-        public DateTime? DateApprove { get; private set; }
-        public bool IsRejected
+        public string RecipientID { get; private set; }
+        public string SendertID { get; private set; }
+        public bool IsRead { get; set; }
+        public DateTime? DateRead { get; set; }
+        public bool IsApproved { get; set; }
+        public DateTime? DateApprove { get; set; }
+        public bool IsRejected { get; set; }
+        public DateTime? DateReject { get; set; }
+
+        public Notification(NotificationType type, string header, string body, string recipientid, string senderid)
         {
-            get { return IsRejected; }
-            set { DateReject = IsRejected ? DateTime.Now : null; }
+            NotifiType = type;
+            Header = header;
+            Body = body;
+            RecipientID = recipientid;
+            SendertID = senderid;
+            IsRead = false;
+            IsApproved = false;
+            IsRejected = false;
         }
-        public DateTime? DateReject { get; private set; }
-
-        //public Notification(NotificationType type, string header, string body)
-        //{        
-        //}
 
         public async Task<int> Send()
         {
-            string sql = $"INSERT INTO notifications (type, header, body) " +
-                $"VALUES ('{(int)Type}','{Header}','{Body}')";
+            string sql = $"INSERT INTO notifications (type, header, body, recipientid, senderid) " +
+                $"VALUES ('{(int)NotifiType}','{Header}','{Body}','{RecipientID}','{SendertID}')";
             long id = await MySQLConnector.Instance().SQLInsert(sql);
             ID = (int)id;
             return ID;
@@ -47,8 +45,16 @@ namespace HyperCube.Models
 
         public async Task Update()
         {
+            int isread = Convert.ToInt32(IsRead);
+            int isapproved = Convert.ToInt32(IsApproved);
+            int isrejected = Convert.ToInt32(IsRejected);
+
+            string dateread = DateRead?.ToString("yyyy-MM-dd HH:mm:ss");
+            string dateapprove = DateApprove?.ToString("yyyy-MM-dd HH:mm:ss");
+            string datereject = DateReject?.ToString("yyyy-MM-dd HH:mm:ss");
+
             string sql = $"UPDATE notifications" +
-                $" SET isread='{IsRead}', dateread='{DateRead}', isapproved='{IsApproved}', dateapprove='{DateApprove}', isrejected='{IsRejected}', datereject='{DateReject}' " +
+                $" SET isread='{isread}', dateread='{dateread}', isapproved='{isapproved}', dateapprove='{dateapprove}', isrejected='{isrejected}', datereject='{datereject}' " +
                 $" WHERE id ='{ID}'";
             await MySQLConnector.Instance().SQLInsert(sql);
         }

+ 1 - 1
MySQLConnector.cs

@@ -35,7 +35,7 @@ namespace HyperCube
 
         public async Task<bool> IsConnect()
         {           
-            string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
+            string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}; convert zero datetime=True";
             if (Connection != null)
                 await SQLSelectComplex("select COUNT(*) from accounts", false);
             else

+ 141 - 0
Pages/ModalNotifications.razor

@@ -0,0 +1,141 @@
+<div class="modal" tabindex="-1" role="dialog" style="display:none" id="@elementid">
+    <div class="modal-container">
+        <div class="modal__body">
+            <div class="modal__body__header">
+                <div class="modal__body__header__info">
+                    <img src="img/user.svg" alt="">
+                    <p>@Title</p>
+                </div>
+            </div>
+            <div class="modal__body__container modal__body__container_noflex">
+                <div class="modal__404__text__box">
+                    @Body
+
+                    <button class="btn_orange" style="margin-bottom: 20px" @onclick="SendTestNotification">Отправить тестовое сообщение этому юзеру</button>
+
+                    <table border="1">
+                        <tr>
+                            <th>Тип</th>
+                            <th>Заголовок</th>
+                            <th>Сообщение</th>
+                            <th>Прочитано</th>
+                            <th>Утверждено</th>
+                            <th>Отклонено</th>
+                        </tr>
+                        @foreach (var n in _notifications)
+                        {
+                            <tr>
+                                <td>@n.Value.NotifiType</td>
+                                <td>@n.Value.Header</td>
+                                <td>@n.Value.Body</td>
+                                <td>
+                                    <input type="checkbox" @bind="n.Value.IsRead" disabled>
+                                    <button @onclick="(() => ReadOnClick(n.Value.ID))">read</button>
+                                </td>
+                                <td>
+                                    <input type="checkbox" @bind="n.Value.IsApproved" disabled>
+                                    <button @onclick="(() => ApproveOnClick(n.Value.ID))">approve</button>
+                                </td>
+                                <td>
+                                    <input type="checkbox" @bind="n.Value.IsRejected" disabled>
+                                    <button @onclick="(() => RejectOnClick(n.Value.ID))">reject</button>
+                                </td>
+                            </tr>
+                        }
+                    </table>
+                </div>
+            </div>
+        </div>
+        <a class="modal_close" style="cursor:pointer" @onclick="@Close">&#10006;</a>
+    </div>
+</div>
+
+@code {
+    @using HyperCube.Models;
+
+    [Inject]
+    public IJSRuntime JsRuntime { get; set; }
+
+    [Parameter]
+    public RenderFragment Title { get; set; }
+
+    [Parameter]
+    public RenderFragment Body { get; set; }
+
+    string message = "";
+    string elementid = "modal_notifications";
+
+    Dictionary<int, Models.Notification> _notifications = new();
+    AccountModel _currentAcc = new();
+
+
+    public void Open(Dictionary<int, Notification> notifications, AccountModel acc)
+    {
+        _notifications = notifications;
+        _currentAcc = acc;
+        JsRuntime.InvokeVoidAsync("OpenModal", elementid);
+    }
+
+    public void Close()
+    {
+        JsRuntime.InvokeVoidAsync("CloseModal", elementid);
+    }
+
+    async Task ReadOnClick(int id)
+    {
+        if (!_notifications[id].IsRead)
+        {
+            _notifications[id].IsRead = true;
+            _notifications[id].DateRead = DateTime.Now;
+        }
+        else
+        {
+            _notifications[id].IsRead = false;
+            _notifications[id].DateRead = null;
+        }
+
+        await _notifications[id].Update();
+    }
+
+    async Task ApproveOnClick(int id)
+    {
+        if (!_notifications[id].IsApproved)
+        {
+            _notifications[id].IsApproved = true;
+            _notifications[id].DateApprove = DateTime.Now;
+        }
+        else
+        {
+            _notifications[id].IsApproved = false;
+            _notifications[id].DateApprove = null;
+        }
+
+        await _notifications[id].Update();
+    }
+
+    async Task RejectOnClick(int id)
+    {
+        if (!_notifications[id].IsRejected)
+        {
+            _notifications[id].IsRejected = true;
+            _notifications[id].DateReject = DateTime.Now;
+        }
+        else
+        {
+            _notifications[id].IsRejected = false;
+            _notifications[id].DateReject = null;
+        }
+
+        await _notifications[id].Update();
+    }
+
+    async Task SendTestNotification()
+    {
+        Notification notification = new(NotificationType.System, "Test from button", "message", _currentAcc.UUID, _currentAcc.UUID);
+
+        int newid = await notification.Send();
+
+        /// adding only in local collection yet
+        _notifications.Add(newid, notification);
+    }
+}

+ 12 - 2
Shared/Sidebar.razor

@@ -9,9 +9,14 @@
                 <div class="top-href__logo" style="cursor:pointer" title="Вернуться на главную" @onclick="@LogoClick">
                     <img src="img/logo_light.svg" alt="">                
                 </div>                
-                <a style="cursor:pointer" title="Личные данные" @onclick="@ProfileClick">
+                <a style="cursor:pointer; position:relative" title="Личные данные" @onclick="@ProfileClick">
                     <img src="img/user.svg" alt="">
-                </a>
+                    @if (_newNotification)
+                    {
+                        <div class="circle_red">
+                        </div>
+                    }                    
+                </a>                
             </div>
             <div class="middle-href">
                 <a style="cursor:pointer" title="Рабочий стол" @onclick="@DesktopClick">
@@ -50,6 +55,11 @@
             <Title></Title>
         </ModalProfile>
 
+        <ModalNotifications @ref="modalNotifications">
+            <Title>Уведомления</Title>
+            <Body></Body>
+        </ModalNotifications>
+
         <ModalFiles @ref="modalFiles">
             <Title></Title>
         </ModalFiles>

+ 47 - 3
Shared/Sidebar.razor.cs

@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Identity;
 using HyperCube.Pages;
 using HyperCube.Models;
 using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
+using Console = HyperCube.Utils.AdvConsole;
 
 namespace HyperCube.Shared
 {
@@ -22,6 +23,7 @@ namespace HyperCube.Shared
         UserManager<IdentityUser> UserManager { get; set; }
 
         ModalProfile modalProfile { get; set; }
+        ModalNotifications modalNotifications { get; set; }
         ModalFiles modalFiles { get; set; }
         ModalCompetency modalCompetency { get; set; }
         ModalRating modalRating { get; set; }
@@ -30,7 +32,10 @@ namespace HyperCube.Shared
         ModalError modalError404 { get; set; }
         ModalLoading modalLoading { get; set; }
 
-        AccountModel _currentAccount;        
+        AccountModel _currentAccount;
+
+        bool _newNotification = false;
+        Dictionary<int, Notification> _notifications = new();
 
         protected override async Task OnInitializedAsync()
         {            
@@ -42,7 +47,9 @@ namespace HyperCube.Shared
             var myFlag = await storage.GetAsync<string>("acc");
             Console.WriteLine($"sidebar myFlag set {myFlag.Value}");
             await storage.SetAsync("completed", true);
-        }
+
+            _newNotification = await CheckNotifications();
+        }       
 
         void LogoClick()
         {
@@ -75,7 +82,10 @@ namespace HyperCube.Shared
 
         void RatingClick()
         {
-            modalRating.Open();
+            //modalRating.Open();
+
+            //tmp
+            modalNotifications.Open(_notifications, _currentAccount);
         }
 
         void RulesClick()
@@ -98,6 +108,40 @@ namespace HyperCube.Shared
             modalError404.Open();            
         }
 
+        async Task<bool> CheckNotifications()
+        {
+            bool newNotifications = false;
+            Notification notification;
+            _notifications.Clear();
+
+            var notifications = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM notifications WHERE recipientid='{_currentAccount.UUID}'");
+            if (notifications.Count > 0)
+            {
+                foreach (var n in notifications)
+                {
+                    notification = new((NotificationType)Convert.ToInt32(n["type"]),
+                        Convert.ToString(n["header"]),
+                        Convert.ToString(n["body"]),
+                        Convert.ToString(n["recipientid"]),
+                        Convert.ToString(n["senderid"]))
+                    {
+                        ID = Convert.ToInt32(n["id"]),
+                        IsRead = Convert.ToBoolean(n["isread"]),
+                        IsApproved = Convert.ToBoolean(n["isapproved"]),
+                        IsRejected = Convert.ToBoolean(n["isrejected"])
+                    };
+                    Console.WriteLine($"add notification. id: {notification.ID}, header: {notification.Header}, body: {notification.Body}");
+
+                    _notifications.Add(notification.ID, notification);
+
+                    if (!notification.IsRead)
+                        newNotifications = true;
+                }                
+            }
+
+            return newNotifications;
+        }
+
         async Task<AccountModel> GetCurrentAcc()
         {
             if (_currentAccount == null)

+ 10 - 0
wwwroot/css/style.css

@@ -1456,3 +1456,13 @@ footer {
         background-color:lightcoral;
         color:lightgrey;    
     }
+
+.circle_red {
+    position: absolute;
+    top: -2px;
+    right: -2px;
+    background: #ff0000;
+    border-radius: 50%;
+    height: 10px;
+    width: 10px;
+}