Strapi v4 how does branch between entityService object - strapi

I am using Strapi v4 and try to branch between entityService object like this:
if (strapi.entityService.findOne("api::post.post", post_id) !== null) {
console.log('update')
} else {
console.log('create')
}
Some of the object null show in terminal but it does not work.
Can anyone help me?

Related

List split into two - not updating Xamarin Forms

I have the below (simplified) model to display and return Chats. On the UI I want to show New Chats and Existing chats separately. When I first load the chats, it works, but if I update a Chat object, the UI doesn't update.
In the Xamarin Forms UI I have a 1) CollectionView binding to NewChats and 2) CollectionView binding to Existing Chats.
I update the IsNew flag programmatically, but that is not reflecting in the UI.
Any thoughts on how to approach this?
public class Chat
{
public string UserId {get;set;}
public bool IsNew {get;set;}
}
private ObservableCollection<Chat> _chats;
public ObservableCollection<Chat> Chats
{
get
{
return _chats;
}
set
{
this._chats= value;
OnPropertyChanged(nameof(Chats));
OnPropertyChanged(nameof(NewChats));
OnPropertyChanged(nameof(ExistingChats));
}
}
public ObservableCollection<Chat> NewChats
{
get
{
if (_chats!= null)
{
return new ObservableCollection<Chat>(_chats.Where(x => x.isNew);
}
return new ObservableCollection<Chat>();
}
}
public ObservableCollection<Chat> ExistingChats
{
get
{
if (_chat!= null)
{
return new ObservableCollection<Chat>(_chats.Where(x => !x.isNew);
}
return new ObservableCollection<Chat>();
}
}
As the comment above says For change notification to occur in a binding between a bound client and a data source, your bound type should either:
Implement the INotifyPropertyChanged interface (preferred).
Provide a change event for each property of the bound type.
you can check here to get offical sample:https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=net-6.0#definition

How to change password masking character in Xamarin forms - Entry

I've currently faced a rather simple issue which eventually put me to a dead end. I am building an application that uses Xamarin Forms and want to change a masking character when user enters password from a bullet to an asterisk.
For entering password I'm using Entry control in Portable lib project in my content page (in VS2017 professional):
<Entry x:Name="Entry_Password" Placeholder="Password" IsPassword="True" />
I know that I probably should create a custom Renderer in Android project for this one, but would really appreciate how to do it for this specific purpose.
Am sure the converter answer will work , but as a personal preference i dont like it. this looks like a renderer job to me .
and here is how i would do it(example only in android because i dont have ios, but its fairly simple to implement it there)
Usage Xamarin forms
<controls:PasswordBox Placeholder="Password"/>
The Renderer (Android)
[assembly: ExportRenderer(typeof(PasswordBox), typeof(PasswordBoxRenderer))]
namespace PasswordAsterisk.Droid.Renderers
{
public class PasswordBoxRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.InputType = Android.Text.InputTypes.TextVariationPassword |
Android.Text.InputTypes.ClassText;
Control.TransformationMethod = new HiddenPasswordTransformationMethod();
}
}
}
internal class HiddenPasswordTransformationMethod : Android.Text.Method.PasswordTransformationMethod
{
public override Java.Lang.ICharSequence GetTransformationFormatted(Java.Lang.ICharSequence source, Android.Views.View view)
{
return new PasswordCharSequence(source);
}
}
internal class PasswordCharSequence : Java.Lang.Object, Java.Lang.ICharSequence
{
private Java.Lang.ICharSequence _source;
public PasswordCharSequence(Java.Lang.ICharSequence source)
{
_source = source;
}
public char CharAt(int index)
{
return '*';
}
public int Length()
{
return _source.Length();
}
public Java.Lang.ICharSequence SubSequenceFormatted(int start, int end)
{
return _source.SubSequenceFormatted(start, end);
}
public IEnumerator<char> GetEnumerator()
{
return _source.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _source.GetEnumerator();
}
}
}
full source code example in GitHub
An easyier way to do this would be to use a Converter to swap every letter in an asterisk and than when you request the value it is plain.
This is an interesting post that could help you with your problem: https://forums.xamarin.com/discussion/52354/is-there-a-way-to-partially-mask-an-entry-field-in-xamarin
It's imported to use oneway and not twoway!
Good luck

How to validate web api response

I am working on a web api where I need to validate response returned. I want to write some generic code to ensure that response returned from api is in a correct format.
Basically there are fields like status, remarks etc which should be present in response if these are null or invalid then i should return some error code but not sure how to achieve this, can someone guide me here? Thanks in advance.
Thanks,
Sudama
The following might work for you. It assumes you're using an ObjectResult though you can adapt it to the IActionResult implementation that you're actually using. The following is not production code; rather, it gives a sense of what you could do.
public class MyResultFilter : IResultFilter
{
public void OnResultExecuted(ResultExecutedContext context)
{
}
public void OnResultExecuting(ResultExecutingContext context)
{
var result = context.Result as ObjectResult;
var value = result.Value as MyCustomType;
if (!IsValid(value)
{
context.Result = new StatusCodeResult(500);
}
}
private bool IsValid(MyCustomType value)
{
return value != null &&
value.Status != null &&
value.Remarks != null;
}
}

ASP.NET Web API Help Pages and Versioning

I would like to create a separate help page for each version of my API. For example, the user could go to /help?v=1 to see version 1.0 routes and /help?v=2 to see version 2.0 routes.
Using SDammann.WebApi.Versioning, I added a Version property to VersionedApiExplorer that will return only the routes for the defined version and added the version as an argument to the constructor. Then I tried this:
config.Services.Add(typeof(IApiExplorer), new VersionedApiExplorer(config, "1"));
config.Services.Add(typeof(IApiExplorer), new VersionedApiExplorer(config, "2"));
But this gives me the following error:
The service type IApiExplorer is not supported.
Parameter name: serviceType
I added just one instance of the service - config.Services.Replace(typeof(IApiExplorer), new VersionedApiExplorer(GlobalConfiguration.Configuration, "1")); - to get the configuration to work, so I could test my help controller. Then tried this:
foreach (var service in Configuration.Services.GetServices(typeof(IApiExplorer))) {
if (service.GetType() != typeof(VersionedApiExplorer)) continue;
var explorer = service as VersionedApiExplorer;
if (explorer.Version == v) {
apiExplorer = explorer;
}
}
This gives the same error I received above. I know I would normally use this.Configuration.Services.GetApiExplorer() but I don't know how I could use that to get the appropriate instance of VersionedApiExplorer. I know I could instantiate the appropriate ApiExplorer directly in the controller, but I would prefer to keep that in my configuration file if possible.
So I have two questions:
How could I add two services of type VersionedApiExplorer to my config object?
How would I retrieve the appropriate service in my help controller?
Or is there a completely different approach I could take to accomplish the same goal?
Thank you!
I ultimately ended up going with the solution I hinted at in my question. I feel like there's a better solution to this problem, but this gets the job done.
First, I added a Version property to VersionedApiExplorer:
public string Version { get; private set; }
Then I modified InitializeApiDescriptions to look like this:
private Collection<ApiDescription> InitializeApiDescriptions()
{
Collection<ApiDescription> apiDescriptions = new Collection<ApiDescription>();
var controllerSelector = configuration.Services.GetHttpControllerSelector();
IDictionary<string, HttpControllerDescriptor> allControllerMappings = controllerSelector.GetControllerMapping();
IDictionary<string, HttpControllerDescriptor> controllerMappings = new Dictionary<string, HttpControllerDescriptor>();
// get only mappings for defined version
if (allControllerMappings != null && Version != null) {
foreach (var key in allControllerMappings.Keys) {
if (key.Substring(0, key.IndexOf('.')) == VersionedControllerSelector.VersionPrefix + Version) {
controllerMappings.Add(key, allControllerMappings[key]);
}
}
}
else if (Version == null) {
controllerMappings = allControllerMappings;
}
if (controllerMappings != null)
{
foreach (var route in configuration.Routes)
ExploreRouteControllers(controllerMappings, route, apiDescriptions);
}
return apiDescriptions;
}
I also added a method I could use to set the Version:
public void SetVersion(string version) {
this.Version = version;
this.apiDescription = new Lazy<Collection<ApiDescription>>(InitializeApiDescriptions);
}
Finally, I modified my HelpController to looks like this:
public ActionResult Index(string v) {
return this.View(GetApiExplorer(v).ApiDescriptions);
}
private IApiExplorer GetApiExplorer(string version) {
if (version == null) {
version = "1";
}
var apiExplorer = this.Configuration.Services.GetApiExplorer() as VersionedApiExplorer;
if (apiExplorer != null) {
apiExplorer.SetVersion(version);
}
return apiExplorer;
}

GWT: current URL of Frame(s)

I have a GWT application that features two frames (com.google.gwt.user.client.ui.Frame). Via Frame.setUrl(...) I can load arbitrary web pages without any problems. Of course, user then can click on links on the loaded pages, which in turn load the corresponding pages? How can I keep track of the currently loaded pages in both frames?
Below are my current attempt using two types of listeners; I found the code snippets on the Web. Both events fire, but still I don't know how to get the current loaded URL
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.ui.Frame;
public class BrowserTabFrame extends Frame implements EventListener {
public BrowserTabFrame() {
super();
sinkEvents(Event.ONLOAD);
addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
System.out.println(event.getSource());
// <iframe style="visibility: visible;" id="ext-gen17" src="http://..." class="gwt-Frame"></iframe>
// however, the src attribute never changes
}
});
}
#Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (DOM.eventGetType(event) == Event.ONLOAD)
System.out.println(event.getCurrentEventTarget());
// [object HTMLIFrameElement]
// no idea what to do with it
}
}
Thanks for any hints!
Christian
The src attribute of an iframe will never change but the URL property of the contained document will. You can get this value using JSNI:
private native String getIframeUrl(IFrameElement frame) /*-{
if (frame.contentDocument !== undefined) {
return frame.contentDocument.URL;
} else if (frame.contentWindow !== undefined &&
frame.contentWindow.document !== undefined)
{
return frame.contentWindow.document;
} else {
return null;
}
}-*/;
...
Window.alert(getIframeUrl(myFrame.getElement()));
However note that, if the security context (any of protocol, domain or port number) of the iframe changes, the browser will not allow the containing application to access the document in the frame.
This is an extension of Jason's solution above. When I tried Jason's code it appeared that the returned URIs are missing hashes (aka fragments, URI suffixes beginning with '#'), and I needed these. After some digging around the DOM spec, the following worked:
public static native String getIframeUri(IFrameElement iframe) /*-{
if (iframe.contentDocument !== undefined) {
if (iframe.contentDocument.defaultView !== undefined
&& iframe.contentDocument.defaultView.location !== undefined) {
return iframe.contentDocument.defaultView.location.href;
} else {
return iframe.contentDocument.URL;
}
} else if (iframe.contentWindow !== undefined
&& iframe.contentWindow.document !== undefined) {
return iframe.contentWindow.document;
} else {
return null;
}
}-*/;
Where the only addition to Jason's code is the nested condition:
if (iframe.contentDocument.defaultView !== undefined
&& iframe.contentDocument.defaultView.location !== undefined) {
return iframe.contentDocument.defaultView.location.href;

Resources