Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.ASPxEditors
Imports DevExpress.Web.ASPxClasses
Partial Public Class VideoView
Inherits System.Web.UI.UserControl
Private Const UserCommentsSessionKey As String = "398f9710-5213-4d77-b3eb-d2a022486bd2", InvalidNameErrorText As String = "Name must be non-empty and its length must not exceed 40 characters.", InvalidCommentErrorText As String = "Comment must be non-empty and its length must not exceed 5000 characters."
Private Const MaxUserNameLength As Integer = 40, MaxCommentLength As Integer = 5000
Private privateVideoTitle As String
Public Property VideoTitle() As String
Get
Return privateVideoTitle
End Get
Set(ByVal value As String)
privateVideoTitle = value
End Set
End Property
Protected ReadOnly Property UserComments() As List(Of Comment)
Get
If Session(UserCommentsSessionKey) Is Nothing Then
Session(UserCommentsSessionKey) = New List(Of Comment)()
End If
Return TryCast(Session(UserCommentsSessionKey), List(Of Comment))
End Get
End Property
Protected Sub InitializeTagList(ByVal video As Video)
rpTags.DataSource = video.TagVideoRelations.Select(Function(r) New With {Key .Name = r.Tag.Name, Key .UrlName = Server.UrlEncode(r.Tag.Name)}).AsQueryable()
rpTags.DataBind()
End Sub
Protected Sub InitializeComments(ByVal video As Video)
Dim persistentComments = video.Comments.AsQueryable()
Dim userComments = Me.UserComments.Where(Function(c) c.VideoID.Equals(video.ID)).AsQueryable()
rpComments.DataSource = persistentComments.Union(userComments).OrderBy(Function(c) c.AddedAt).Select(Function(c) New With {Key .UserName = c.UserName, Key .UserAvatarID = c.UserAvatarID, Key .AddedAt = String.Format("{0:d}", c.AddedAt), Key .Text = c.Text})
rpComments.DataBind()
End Sub
Protected Sub InitializeRelatedVideos(ByVal video As Video)
Dim videoTagNames = video.TagVideoRelations.Select(Function(r) r.Tag.Name).ToList()
'Map
'Reduce
rpRelatedVideos.DataSource = New VideoPortalDataContext().Videos.Where(Function(v) v.ID <> video.ID).SelectMany(Function(v) v.TagVideoRelations, Function(v, r) New With {Key .Video = v, Key .TagName = r.Tag.Name}).Where(Function(vt) videoTagNames.Contains(vt.TagName)).GroupBy(Function(vt) vt.Video.ID, Function(vt) vt.Video, Function(i, vc) New With {Key .Video = vc.First(), Key .Relevance = vc.Count()}).OrderByDescending(Function(vr) vr.Relevance).AsEnumerable().Select(Function(vr) New With {Key .ID = vr.Video.ID, Key .Title = vr.Video.Title, Key .Url = ResolveUrl("~/Watch/" & Server.UrlEncode(vr.Video.Title))})
rpRelatedVideos.DataBind()
End Sub
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim video = New VideoPortalDataContext().Videos.Where(Function(v) v.Title = VideoTitle).First()
InitializeTagList(video)
InitializeComments(video)
InitializeRelatedVideos(video)
videoPlaceholder.InnerHtml = video.EmbeddedCode
videoTitleControl.InnerText = VideoTitle
product.InnerHtml = video.Product
published.InnerHtml = String.Format("{0:d}", video.CreationDate)
description.InnerHtml = video.Description
If (Not cpComments.IsCallback) Then
commentEditor.Html = String.Empty
tbName.Text = String.Empty
End If
End Sub
Protected Function ValidateComment(ByVal userName As String, ByVal text As String) As Boolean
Dim valid = True
If String.IsNullOrEmpty(userName) OrElse userName.Length > MaxUserNameLength Then
errorText.InnerText = InvalidNameErrorText
valid = False
End If
If String.IsNullOrEmpty(text) OrElse text.Length > MaxCommentLength Then
If (Not valid) Then
errorText.InnerText += Constants.vbLf
End If
errorText.InnerText += InvalidCommentErrorText
valid = False
End If
errorFrame.Visible = Not valid
Return valid
End Function
Protected Sub cpComments_Callback(ByVal sender As Object, ByVal e As CallbackEventArgsBase)
Dim video = New VideoPortalDataContext().Videos.Where(Function(v) v.Title = VideoTitle).First()
Dim userName = tbName.Text.Trim()
Dim text = e.Parameter.Trim()
If (Not ValidateComment(userName, text)) Then
Return
End If
Dim comment As New Comment() With {.VideoID = video.ID, .UserAvatarID = 0, .UserName = Server.HtmlEncode(userName), .Text = commentEditor.Html, .AddedAt = DateTime.Now}
UserComments.Add(comment)
InitializeComments(video)
commentEditor.Html = String.Empty
tbName.Text = String.Empty
End Sub
End Class