Sei sulla pagina 1di 3

La classe Dal :

Imports System.Security.Cryptography
Public Class Dal
Implements IDal
Public Bdd As BddContext
Public Sub New()
Bdd = New BddContext()
End Sub

Public Function ObtientTousLesRestaurants() As List(Of Resto) Implements


IDal.ObtientTousLesRestaurants
Return Bdd.Restos.ToList()
End Function
Public Sub CreerRestaurant(ByVal n As String, ByVal t As String) Implements
IDal.CreerRestaurant

Bdd.Restos.Add(New Resto With {


.Nom = n,
.Telephone = t
})
Bdd.SaveChanges()
End Sub
Public Sub IDisposable_Dispose() Implements IDisposable.Dispose
Bdd.Dispose()
End Sub
Public Sub ModifierRestaurant(ByVal id As Integer, ByVal nom As String, ByVal
telephone As String) Implements IDal.ModifierRestaurant
Dim restoTrouve As Resto = Bdd.Restos.FirstOrDefault(Function(resto)
resto.Id = id)

If restoTrouve IsNot Nothing Then


restoTrouve.Nom = nom
restoTrouve.Telephone = telephone
Bdd.SaveChanges()
End If
End Sub
Public Function RestaurantExiste(ByVal nom As String) As Boolean Implements
IDal.RestaurantExiste
Return Bdd.Restos.Any(Function(resto) String.Compare(resto.Nom, nom,
StringComparison.CurrentCultureIgnoreCase) = 0)
End Function
Public Function AjouterUtilisateur(ByVal nom As String, ByVal motDePasse As
String) As Integer Implements IDal.AjouterUtilisateur
Dim motDePasseEncode As String = EncodeMD5(motDePasse)
Dim utilisateur As Utilisateur = New Utilisateur With {
.Prenom = nom,
.MotDePasse = motDePasseEncode
}
Bdd.Utilisateurs.Add(utilisateur)
Bdd.SaveChanges()
Return utilisateur.Id
End Function
Public Function Authentifier(ByVal nom As String, ByVal motDePasse As String) As
Utilisateur Implements IDal.Authentifier
Dim motDePasseEncode As String = EncodeMD5(motDePasse)
Return Bdd.Utilisateurs.FirstOrDefault(Function(u) u.Prenom = nom AndAlso
u.MotDePasse = motDePasseEncode)
End Function
Public Function ObtenirUtilisateur(ByVal id As Integer) As Utilisateur
Implements IDal.ObtenirUtilisateur
Return Bdd.Utilisateurs.FirstOrDefault(Function(u) u.Id = id)
End Function
Public Function ObtenirUtilisateur(ByVal idStr As String) As Utilisateur
Implements IDal.ObtenirUtilisateur
Dim id As Integer
If Integer.TryParse(idStr, id) Then Return ObtenirUtilisateur(id)
Return Nothing
End Function

Private Function EncodeMD5(ByVal motDePasse As String) As String


Dim motDePasseSel As String = "ChoixResto" & motDePasse & "ASP.NET MVC"
Return BitConverter.ToString(New
MD5CryptoServiceProvider().ComputeHash(ASCIIEncoding.[Default].GetBytes(motDePasseSe
l)))
End Function
Public Function CreerUnSondage() As Integer Implements IDal.CreerUnSondage
Dim sondage As Sondage = New Sondage With {
.SDate = DateTime.Now
}
Bdd.Sondages.Add(sondage)
Bdd.SaveChanges()
Return sondage.Id
End Function

Public Sub AjouterVote(ByVal idSondage As Integer, ByVal idResto As Integer,


ByVal idUtilisateur As Integer) Implements IDal.AjouterVote
Dim vote As Vote = New Vote With {
.Resto = Bdd.Restos.First(Function(r) r.Id = idResto),
.Utilisateur = Bdd.Utilisateurs.First(Function(u) u.Id = idUtilisateur)
}
Dim sondage As Sondage = Bdd.Sondages.First(Function(s) s.Id = idSondage)
If sondage.Votes Is Nothing Then sondage.Votes = New List(Of Vote)()
sondage.Votes.Add(vote)
Bdd.SaveChanges()
End Sub

Public Function ObtenirLesResultats(ByVal idSondage As Integer) As List(Of


Resultats) Implements IDal.ObtenirLesResultats
Dim restaurants As List(Of Resto) = ObtientTousLesRestaurants()
Dim resultats As List(Of Resultats) = New List(Of Resultats)()
Dim sondage As Sondage = Bdd.Sondages.First(Function(s) s.Id = idSondage)

For Each grouping As IGrouping(Of Integer, Vote) In


sondage.Votes.GroupBy(Function(v) v.Resto.Id)
Dim idRestaurant As Integer = grouping.Key
Dim resto As Resto = restaurants.First(Function(r) r.Id = idRestaurant)
Dim nombreDeVotes As Integer = grouping.Count()
resultats.Add(New Resultats With {
.Nom = resto.Nom,
.Telephone = resto.Telephone,
.NombreDeVotes = nombreDeVotes
})
Next

Return resultats
End Function

Public Function ADejaVote(ByVal idSondage As Integer, ByVal idStr As String) As


Boolean Implements IDal.ADejaVote
Dim id As Integer

If Integer.TryParse(idStr, id) Then


Dim sondage As Sondage = Bdd.Sondages.First(Function(s) s.Id =
idSondage)
If sondage.Votes Is Nothing Then Return False
Return sondage.Votes.Any(Function(v) v.Utilisateur IsNot Nothing AndAlso
v.Utilisateur.Id = id)
End If

Return False
End Function
End Class

Potrebbero piacerti anche