My project tracking system needs a bit of work. Currently, every user can assign themselves a bunch of projects and track their hours on said projects.
I'm trying to add a where condition to my list of options where people can add their hours. Currently, people can select all projects. That can be a bit tiresome with lots of projects, so I want a Where
statement to limit their options to their own projects.
Here's the problematic piece of code:
.Field(new Field("ProjectEntry.IdProject")
.Options(new Options()
.Table("View_User_Project")
.Value("IdProject")
.Label("Name")
.Where("View_User_Project", "View_User_Project.IdUser", "=", idUser) // TODO Find out what's going on
)
.Validator(Validation.NotEmpty())
)
This "Where" method only takes one argument, an Action<Query>
kind of argument. I've never seen an example for a Where
statement with that kind of syntax.
Could someone provide me with an example?
Full DB code:
var context = HttpContext.Current;
var idUser = Utility.GetIdUser(context);
var request = context.Request;
using (var db = Utility.GetDb())
{
var editor = new Editor(db, "ProjectEntry", "IdProjectEntry")
.Model<ProjectEntryModel>("ProjectEntry")
.Model<ProjectModel>("Project")
.Field(new Field("ProjectEntry.IdProject")
.Options(new Options()
.Table("View_User_Project")
.Value("IdProject")
.Label("Name")
.Where("View_User_Project", "View_User_Project.IdUser", "=", idUser) // TODO Find out what's going on
)
.Validator(Validation.NotEmpty())
)
.Field(new Field("ProjectEntry.IdUser"))
.Field(new Field("ProjectEntry.TimeWorked")
.Validator(Validation.NotEmpty())
.Validator(Validation.Numeric())
)
.Field(new Field("ProjectEntry.TimestampWeek")
.Validator(Validation.NotEmpty())
)
.LeftJoin("Project", "Project.IdProject", "=", "ProjectEntry.IdProject")
.Where("ProjectEntry.IdUser", idUser, "=");
editor.PreCreate += (sender, e) => editor.Field("ProjectEntry.IdUser").SetValue(idUser);
var response = editor.Process(request).Data();
return Json(response);
}
Well, that was short. Turns out you need an anonymous function to edit the query object.
Example:
.Field(new Field("UserProject.IdProject")
.Options(new Options()
.Table("View_User_Project")
.Value("IdProject")
.Label("Name")
.Where((q) =>
{
q.Where("View_User_Project.IdUser", idUser);
}
)
)
.Validator(Validation.NotEmpty())
)