How to loop through Membership List? - asp.net-mvc

I have a list of all memberships:
Dim allUsers = Membership.GetAllUsers().Cast(Of MembershipUser).ToList
Despite my searches, I have been unable to figure out how to loop through this to do something like the following. I want to loop through and retrieve a record, if the record contains a certain value, add the user to a new collection.
for each allusers as user
' get user profile
' if user profile has a certain value, add it to another collection
end for each
Then, once the new collection is full, output that collection to my view.
Does that make sense? How can I do this? Thank you.

Try this:
Public Function GetUsers() As IEnumerable(Of MembershipUser)
Dim newListOfUsers = New List(Of MembershipUser)()
Dim users As MembershipUserCollection = Membership.GetAllUsers()
For Each user As MembershipUser In users
If user.Comment = "A Leather Glove" Then
newListOfUsers.Add(user)
End If
Next
Return newListOfUsers
End Function

Added C# version:
var newListOfUsers = new List<MembershipUser>();
Membership.GetAllUsers().OfType<MembershipUser>().ToList().ForEach( user =>
{
if(user.Comment.Equals("A Leather Glove")) newListOfUsers.Add(user);
});

Related

Returning a specific string format from an array

I have an array of Persons (class contain name and lastname and id )
what I have to do is to return a string form this array but in a specific format an example will be more explicative
array=[PERS1,PERS2]
I need this as a return value : "The name of all persons : "+ PERS1.name + PERS1.LASTN + " , " + PERS2.name +PERS2.LASTN +","
I know this method
array.each{ |per|
#but this will not return the format ,and with each I think I can only print (I'm new in the ruby field
}
all of this because I need it when overriding to_s , because I need to provide a string -> to_s
def to_s
"THE name of all preson"+#array.each #will not work as I want
end
Thank you for ur time and effort and if you need any clarification please let me know
each just iterates over a collection and returns the collection itself. You may want to use map and join the result.
array.map { |person| "#{person.name} #{person.lastn}" }.join(',')
Or if you modify your Person class it can be even simpler.
# I assume that the name of the class is Person and name and lastn are always present
class Person
def full_name
"#{person.name} #{person.lastname}"
end
end
# Then you can call this method on `map`.
array.map(&:full_name).join(',')
Try this,
array.each do |per|
"#{per.name} #{per.LASTN}"
end
For more info check Interpolation

How to access property from grails query

I have a domain class called Application as follows:
class Application {
static hasOne = [resumption:Resumption, employee:Employee]
//Employee employee
Date startDate
Date endDate
Integer amountOfDays
String leaveRecommended
String leaveNotRecommended
Date supervisorDate
String toString(){
return "Application for ${employee.lastName}, ${employee.firstName}"
}
}
In the ApplicationController I'm trying to write a query that is going to find all applications that match a particular employee id. I do so as follows:
def applicationlist(){
if(!params.max){
params.max = 10
}
def query
def criteria = Application.createCriteria()
def results
query = { eq("employee_id", Long.parseLong("1")) }
results = criteria.list(params, query)
render(view:"employeeapplicationlist", model:[applicationlist:results])
}
Now I keep getting the error: "could not resolve property: employee_id"
I've checked the generated Application table in MySql, there is a column called employee_id with a value. The weird thing is I can access any other property (like amountOfDays), so what's the deal with employee_id? Why is it complaining that it cannot resolve the property? What am I missing? Thanks in advance.
Associations in the criteria DSL are of the form
Application.withCriteria{
employee{
eq 'id', 1
}
}
http://grails.org/doc/latest/guide/GORM.html#criteria
But you could probably just do:
def employee = Employee.proxy(1)
Application.findAllByEmployee( employee )
This appears a few times in the Grails User Guide as 'querying associations'
Oh well it looks like I'm still not fully adjusted to interfacing with the database on an Object level. For anyone else with this or a similar problem, here's the fix:
query = { eq("employee.id", Long.parseLong("1")) }
Sine the Application Domain class has one Employee, then we just need to access the id field of that employee. Remember we're in the ApplicationController.

Rails: Find and use the id number from a diff controller

I want to extract the id number of a unique record, that resides in a different controller as an integer, so I can save it as part of a new record in a new controller.
I can't seem to get the id to shed it's 'Array' attribute.
I've been using this:
class MessagesController < ApplicationController
def incoming
a = Group.where("name = ?", name).map { |n| n.id }
group_number = a.id
puts "#{group_number} is the number!"
end
output is always [2] or [3] or whatever.
adding this doesn't seem to cure it
group_as_int = group_number.to_i
Lastly, the reason I'm doing all this is to save that group number as a new record in a third controller, like this:
Subscriber.create(:group_id => group_number, :active => "1")
or
Subscriber.create(:group_id => group_as_int, :active => "1")
Of course, the create balks when I try to pass an array into the create function.
Thoughts?
You are trying to put business logic into the controller.
Try to refactor your methods and put them into your models instead.
Beside that you get the number in the following way:
group = Group.where("name = ?", name).first
group_number = group.id if group.present?
You might want to try .first to get the integer out of the array.
I will try to explain from your code what you did wrong.
The first line:
matching_group_ids = Group.where("name = ?", name).map { |n| n.id }
You called it a, but i prefer more verbose names. matching_group_ids now holds an array of id's. To get the first value of this array, the easiest solution is to just write
group_number = matching_group_ids[0]
or, more readable:
group_number = matching_group_ids.first
Mind you: you should test that the returned array is not empty.
Hope this helps.

many-to-many, poco, ef4

I have 3 entities:
Goods [GID(PK), GoodName]
Persons [PID(PK), PersonName]
Roles [RID(PK), RoleName]
But now I need to associate these object with each other.
In other words, each Good can have MANY Persons in MANY Roles.
I have a table in DB with 3 fields (GID, PID, RID)
For example:
Book (GID#1), can have 3 associated persons:
1. Jack (PID#1) in role Author (RID#1)
2. Jack (PID#1) in role Editor (RID#2)
3. Bill (PID#2) in role Painter (RID#3)
How can I map this in POCO format in Entity Framework 4?
I believe you have to create another PersonRoles header where you store the Person-Role relation, then you access the person+role via this one:
PersonRoles [PRID(PK), PersonName, RoleName] (note: you could also do this withnow entitykey, just relationships, EF will eliminate this entity and give a direct Person.Roles entity, which you can do access it via Book.Persons.Select((p) p.Roles)).
PersonRole#1: Jack#1/Author#1
PersonRole#2: Jack#1/Editor#2
PersonRole#3: Bill#2/Painter#3
Book.PersonRole = context.PersonRoles.
SingleOrDefault((pr) => pr.Person.PersonId == 1 && pr.RoleId == 1);
Note: my main lang is VB.NET so I apologize for the pseudu code above, but I hope you get the idea.
Update
It should be like:
<DataContract(IsReference:=True)>
<KnownType(GetType(Good))>
<KnownType(GetType(Person))>
<KnownType(GetType(Role))>
Partial Public Class GoodPersonRole
Implements IObjectWithChangeTracker
Implements INotifyPropertyChanged
<DataMember()>
Public Property GoodId() As Integer
Get
Return _goodId
End Get
Set(ByVal value As Integer)
If Not Equals(_goodId, value) Then
If ChangeTracker.ChangeTrackingEnabled AndAlso ChangeTracker.State <> ObjectState.Added Then
Throw New InvalidOperationException("The property 'GoodId' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.")
End If
If Not IsDeserializing Then
If Good IsNot Nothing AndAlso Not Equals(Good.GoodId, value) Then
Good = Nothing
End If
End If
_goodId = value
OnPropertyChanged("GoodId")
End If
End Set
End Property
Private _goodId As Integer
<DataMember()>
Public Property Good() As Good
Get
Return _good
End Get
Set(ByVal value As Good)
If _good IsNot value Then
If ChangeTracker.ChangeTrackingEnabled AndAlso ChangeTracker.State <> ObjectState.Added AndAlso value IsNot Nothing Then
' This the dependent end of an identifying relationship, so the principal end cannot be changed if it is already set,
' otherwise it can only be set to an entity with a primary key that is the same value as the dependent's foreign key.
If Not Equals(GoodId, value.GoodId) Then
Throw New InvalidOperationException("The principal end of an identifying relationship can only be changed when the dependent end is in the Added state.")
End If
End If
Dim previousValue As Good = _good
_good = value
FixupGood(previousValue)
OnNavigationPropertyChanged("Good")
End If
End Set
End Property
Private _good As Good
End Class
(part from the generated entity by ADO.NET VB POCO Entity Generator)
I just copied the 'Good' Id and nav. property but it should be the three of them.

Unit testing controller actions with complex viewModels

I'm just getting going with ASP.NET MVC and I'm also new to unit testing :) So far, so good.
I have a controller action that sets up an index view using a viewmodel. Testing the controller action is straight-forward as I can pass a fake service class in the controller's constructor, but my viewmodel is quite complex and fetches it's own service class on instantiation.
Code should make this clearer I hope...
Controller action:
Function Index(ByVal id As Integer?) As ActionResult
Dim totalCount As Integer = 0
Dim selectedClient As Integer
If id Is Nothing Then
selectedClient = _portalClientService.GetFirstClient().ID
Else
selectedClient = id
End If
Dim users As MembershipUserCollection = _membershipService.GetUsersByClientId(selectedClient, 0, 1000, totalCount)
Return View(New UserListViewModel(users, selectedClient))
End Function
Viewmodel class:
Public Class UserListViewModel
Private _clientService As IPortalClientService
Public Sub New(ByVal users As MembershipUserCollection, ByVal selectedClient As Integer)
Me.New(users, selectedClient, Nothing)
End Sub
Public Sub New(ByVal users As MembershipUserCollection, ByVal selectedClient As Integer, ByVal clientService As IPortalClientService)
_users = users
_clientService = If(clientService, New PortalClientService)
_clients = New SelectList(_clientService.GetClients.OrderBy(Function(c) c.ClientName), "ID", "ClientName", selectedClient)
End Sub
Private _users As MembershipUserCollection
Public Property Users() As MembershipUserCollection
Get
Return _users
End Get
Set(ByVal value As MembershipUserCollection)
_users = value
End Set
End Property
Private _clients As SelectList
Public Property Clients() As SelectList
Get
Return _clients
End Get
Set(ByVal value As SelectList)
_clients = value
End Set
End Property
End Class
EDIT:
When testing the controller action, how do I get the viewmodel to use a fake service class?
Should I just ditch the first constructor and always pass in the service from the controller or is there another way?
Cheers,
Nick
I am probably splitting hair, but I would say your model is more a domain model than a view model. Remove the dependency to the IPortalClientService, or at least do not let the model instantiate it by itself.
I prefer to remove such dependencies away from the view, and over to the controller.
Actually, this is a pattern that we use all the time in our public facing API's, and demonstrates good use of dependency injection. I would pass this in a code review with no problems.
Your implementation gives the user the option to flexibly create the object, and provides for testability.
The only "problem" is that your tests can't easily cover the one line of code in the first constructor, but that's only a problem if you have someone who's fanatical about code coverage - which is usually a problem in itself.

Resources