How to I write ModelBinder that access all fields by prefix? - asp.net-core

For example, I like my ModelBinder to transform all "field_???" values to my single custom object.
How I could do that?
ValueProvider.GetValue only allows me to access by exact name, and I can't enumerate all possible fields...

Try this:
public IActionResult OnPost([Bind(Prefix = "field_")] Whatever model)
Read the article https://learn.microsoft.com/ru-ru/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0

Related

Exclude controller from route with string param .NET core

I want to have an endpoint that looks like: localhost:5000/abc123
This is basically to replicate the functionality of tinyurl.
Controller
[HttpGet("/{myString}")]
public async Task<IActionResult> Get(string myString)
{}
This works but all files now come through this contoller eg: localhost:5000/runtime.js etc
Is this possible to do only for certain strings?
Use Route constraint to filter values for myString
For example, if a file name is a string containing a dot . is a valid suggestion in your case, you can use the following regex to accept alphanumeric strings
[HttpGet("/{myString::regex(^\\w+$)}")]

Output Laravel SQL Column as a link?

I have a Javascript table pulling data into it from a Laravel Json response, I have the column 'name' I wish to turn into a link when it is in the table? Is there anyway to do it like this:
"SELECT CONCAT('<a href=computer/?id=',id,'>',name,'</a>') as name,
I would rather use eloquent rather than straight SQL.
I really wouldn't advise building up HTML as part of your SQL query. Say you want to use that JSON feed somewhere else that doesn't support HTML in the field or you want to append a class to link you've got to change your code.
However there is nothing to wrong with building the link and passing that through you could consider using the Appending Values To JSON functionality of eloquent model.
So you could do something like
class Product extends Model
{
/**
* The accessors to append to the model's array form.
*
* #var array
*/
protected $appends = ['link'];
public function getLinkAttribute()
{
return route('routeName', ['id' => $this->id]);
}
}
If you really must pass back an anchor tag then you could use the link_to_route method which is part of the laravelcollective/html package.

Give an alias to field name on ::find('all')

I have a field on Database called name but when I retrieve this I would that be called label.
In raw SQL I can do that like this:
SELECT name AS label FROM ...
Theres a "quick way" to do this on find without need to deal with entity, virtual fields etc...
I did it using virtual properties like this on Entity:
protected $_virtual = ['label'];
protected function _getlabel()
{
return $this->_properties['name'];
}
A potential gotcha could be... I'm was using _serialize to return the result and it was not working because you have to expose the virtual properties using $_virtual.

Need a concept on fetching data with HQL while three or more tables are in use

A small briefing on what I am trying to do.
I have three tables Content(contentId, body, timeofcreation), ContentAttachmentMap(contentId, attachmentId) and Attachment(attachmentId, resourceLocation).
The reason I adopted to create the mapping table because in future application the attachment can also be shared with different content.
Now I am using HQL to get data. My objectives is as follows:
Get All contents with/without Attachments
I have seen some examples in the internet like you can create an objective specific class (not POJO) and put the attribute name from the select statement within its constructor and the List of that Class object is returned.
For e.g. the HQL will be SELECT new com.mydomain.myclass(cont.id, cont.body) ..... and so on.
In my case I am looking for the following SELECT new com.mydomain.contentClass(cont.id, cont.body, List<Attachment>) FROM ...`. Yes, I want to have the resultList contain contentid, contentbody and List of its Attachments as a single result List item. If there are no attachments then it will return (cont.id, contentbody, null).
Is this possible? Also tell me how to write the SQL statements.
Thanks in advance.
I feel you are using Hibernate in a fundamentally wrong way. You should use Hibernate to view your domain entity, not to use it as exposing the underlying table.
You don't need to have that contentClass special value object for all these. Simply selecting the Content entity serves what you need.
I think it will be easier to have actual example.
In your application, you are not seeing it as "3 tables", you should see it as 2 entities, which is something look like:
#Entity
public class Content {
#Id
Long id;
#Column(...)
String content;
#ManyToMany
#JoinTable(name="ContentAttachmentMap")
List<Attachment> attachments;
}
#Entity
public class Attachment {
#Id
Long id;
#Column(...)
String resourceLocation
}
And, the result you are looking for is simply the result of HQL of something like
from Content where attachments IS EMPTY
I believe you can join fetch too in order to save DB access:
from Content c left join fetch c.attachments where c.attachments IS EMPTY

Use enum to select string from wicket properties file

I'd like to add a label to a wicket panel where the label's model is an enum value. Based on the value of that enum, I'd like to display a different message pulled from the page's properties file.
For example, here's an enum:
public enum ApprovalType { UNAPPROVED, APPROVED, BLOCKED };
I can easily add a label to the panel that has this enum as its model:
add(new Label("approval", new Model(ApprovalType.APPROVED)));
However, this will simply output UNAPPROVED, APPROVED, or BLOCKED. I'd like to output a localized message that is selected based on the value of this enum.
So if I have this properties file, what's the best way to get these messages to output?
UNAPPROVED=Your membership is currently pending approval
APPROVED=Your membership has been approved
BLOCKED=You have been blocked from membership
I can get it working like this:
add(new Label("approval", new ResourceModel(ApprovalType.APPROVED.name());
But the problem is that ResourceModel only accepts a string (resource key). I'd like to pull the value of the resource key automatically from a model (preferrably a CompoundPropertyModel). In other words, I don't want to do this:
Member member = (Member) getDefaultModelObject();
add(new Label("approval", new ResourceModel(member.getApproval().name())));
I'd rather do something like:
add(new EnumLabel("approval"); // assuming I have a CompoundPropertyModel
Is there a component that does this that comes with Wicket? Or do I need to write my own?
Lastly, I discovered this also works:
add(new Label("approval", new StringResourceModel(
"${}",
new PropertyModel<ApprovalType>(getDefaultModel(),"approval")
)));
But this seems overly verbose, and still doesn't utilize the simplicity of using CompoundPropertyModel.
Ideas?
Not sure if this 'll work, but maybe you could try writing your own EnumModel class that takes the value of an Enum and returns a ResourceModel value..
Please say so if the above isn't clear enough, I'll try to elaborate.