I have a array of thread objects, each thread object with the properties
unit:number
task:number
subtask:number
I want to create a pipe to filter after these threads, so far I have a working pipe like below. I'm not really satisfied with it yet and wanted to ask you guys if there is a more elegant solution?
HTML:
<div class="thread-item" *ngFor="#thread of threadlist | threadPipe:unitPipe:taskPipe:subtaskPipe"></div>
Pipe.ts
export class ThreadPipe implements PipeTransform{
threadlistCopy:Thread[]=[];
transform(array:Thread[], [unit,task,subtask]):any{
//See all Threads
if(unit == 0 && task == 0 && subtask == 0){
return array
}
//See selected Units only
if(unit != 0 && task == 0 && subtask == 0){
this.threadlistCopy=[];
for (var i = 0; i<array.length;i++){
if(array[i].unit == unit){
this.threadlistCopy.push(array[i])
}
}
return this.threadlistCopy
}
//See selected Units and Tasks
if (unit != 0 && task != 0 && subtask == 0){
this.threadlistCopy=[];
for (var i = 0; i<array.length;i++){
if(array[i].unit == unit && array[i].task == task){
this.threadlistCopy.push(array[i])
}
}
return this.threadlistCopy
}
// See selected units, tasks, subtask
if (unit != 0 && task != 0 && subtask != 0){
this.threadlistCopy=[];
for (var i = 0; i<array.length;i++){
if(array[i].unit == unit && array[i].task == task && array[i].subtask == subtask){
this.threadlistCopy.push(array[i])
}
}
return this.threadlistCopy
}
}
}
You are implementing your pipe the right way, but you are basically re-inventing the Array.prototype.filter mechanism in your code. A simpler way will be:
export class ThreadPipe implements PipeTransform{
transform(array:Thread[], [unit,task,subtask]):any{
//See all Threads
if(unit == 0 && task == 0 && subtask == 0){
return array
}
//See selected Units only
if(unit != 0 && task == 0 && subtask == 0){
return array.filter(thread => {
return thread.unit === unit;
});
}
//See selected Units and Tasks
if (unit != 0 && task != 0 && subtask == 0){
return array.filter(thread => {
return thread.unit === unit && thread.task === task;
});
}
// See selected units, tasks, subtask
if (unit != 0 && task != 0 && subtask != 0){
return array.filter(thread => {
return thread.unit === unit && thread.task === task && thread.subtask === subtask;
});
}
}
}
Related
please help me out, i want to perform this conditional statement is one or two of this conditional statement will be through but none of the expression i provide will be save to database... Thanks
This are the function that i called
public function updateuserstage($userid){
$user = User::where("user_id",$userid)->first();
//get the stage bonus for this stage
$stageBonus = StageBonus::where("user_id",$userid)-
where("stage",$user->stage)->first();
//get the completion bonus
$bonus = $this->completionbonus($user->stage);
$directdownline = $this->loaduserdirectdownlines($userid);
// $downlineright = $this-
loaduserdirectdownlines($directdownline[0]->user_id);
// $downlineleft = $this-
loaduserdirectdownlines($directdownline[1]->user_id);
$downlineright = $this-
loaduserdirectdownlinesrightleft($directdownline[0]->user_id);
$downlineleft = $this-
loaduserdirectdownlinesrightleft($directdownline[1]->user_id);
// matching bonus reward
if(($user->boosted == 1) && ($directdownline[0]->boosted == 1) &&
($directdownline[1]->boosted == 1) && ($downlineright == 2) &&
($downlineleft == 2)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimapv']))
$stageBonus->point = $stageBonus-
point+$bonus['ultimapv'];
if (isset($bonus['cash']))
$stageBonus->cash = $stageBonus->cash+$bonus['cash'];
// direct downline reward
This function here return true when i log it outside loop but never save to database in here
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
1) || ($directdownline[1]->boosted == 1)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['totalultimarefbonus']))
$stageBonus->cash = $stageBonus-
cash+$bonus['totalultimarefbonus'];
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
1) || ($directdownline[1]->boosted == 0)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimarefbonus']))
$stageBonus->cash = $stageBonus-
cash+$bonus['ultimarefbonus'];
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
0) || ($directdownline[1]->boosted == 1)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimarefbonus']))
$stageBonus->cash = $stageBonus-
>cash+$bonus['ultimarefbonus'];
} else if(($user->boosted == 1) && ($downlineright[0]->boosted ==
1) || ($downlineright[1]->boosted == 1) || ($downlineleft[0]-
>boosted == 1) || ($downlineleft[1]->boosted == 1)){
if (isset($bonus['ultimarefbonuschild']))
$stageBonus->cash = $stageBonus-
>cash+$bonus['ultimarefbonuschild'];
// referral reward for parent user if boosted
} else if($user->boosted == 1){
if (isset($bonus['refbonus']))
$stageBonus->cash = $stageBonus->cash+$bonus['refbonus'];
// referral reward for user that is not boosted
} else if($user->boosted == 0){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['refbonus']))
$stageBonus->cash = $stageBonus->cash+$bonus['refbonus'];
}
$stageBonus->completed = 1;
$stageBonus->completed_on = Carbon::now();
$stageBonus->save();
//increase user stage
$user->stage+= 1;
$user->save();
}
}
This is the function that will determine what to save in those expression
public function completionbonus($stage){
switch($stage){
case 1:
return [ 'voucher'=>600, 'cash'=>5000, 'point'=>100, 'refbonus'=>2000, 'totalultimarefbonus'=>4000, 'ultimarefbonus'=>2000, 'ultimarefbonuschild'=>1500, 'ultimapv'=>700];
break;
}
}
This is the model
Schema::create('stage_bonuses', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->bigInteger('point')->default(0);
$table->bigInteger('cash')->default(0);
$table->date('date_entered')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('user_id')->on('users');
});
Thank you very much i really appreciate your effort
Have actually got the answer the condition is returning two or true and if is returning two or more true it will not get save to database... Thanks
how do I use two pipes sequentially?
<div class="thread" *ngFor="thread of threadlist | bookmarkPipe | threadPipe"></div>
In specific my threads have a bookmark:boolean property as well as tag properties (unit,task,subtask).
So what I want to achieve is that the first pipe filters all threads which are bookmarked, then apply the 2nd pipe (below)
export class ThreadPipe implements PipeTransform{
transform(array:Thread[], [unit,task,subtask]):any{
//See all Threads
if(unit == 0 && task == 0 && subtask == 0){
return array
}
//See selected Units only
if(unit != 0 && task == 0 && subtask == 0){
return array.filter(thread => {
return thread.unit === unit;
});
}
//See selected Units and Tasks
if (unit != 0 && task != 0 && subtask == 0){
return array.filter(thread => {
return thread.unit === unit && thread.task === task;
});
}
// See selected units, tasks, subtask
if (unit != 0 && task != 0 && subtask != 0){
return array.filter(thread => {
return thread.unit === unit && thread.task === task && thread.subtask === subtask;
});
}
}
}
In fact, there is nothing special to do. The second pipe will receive the value from the previous one:
data -> Pipe1 -> filtered data (#1) -> Pipe2 -> filtered data (#2)
For example, if I have an array [ 'val1', 'val2', 'val3' ] and with the following pipes:
#Pipe({
name:'pipe1'
})
export class Pipe1 {
transform(data) {
return data.filter((d) => {
return d!= 'val1';
});
}
}
#Pipe({
name:'pipe2'
})
export class Pipe2 {
transform(data) {
return data.filter((d) => {
return d!= 'val2';
});
}
}
By using the following expression in an ngFor:
#elt of data | pipe1 | pipe2
data | pipe1 will return [ 'val2', 'val3' ] and return [ 'val3' ] if we apply the pipe2 on the previous result.
See the corresponding plunkr: https://plnkr.co/edit/EpKMitR3w89fRiyGYQz7?p=preview.
I was recently asked in the interview
What would be the efficient algorithm to find if two given binary trees are structurally identical but not the content?
a
/ \
b c
\
e
z
/ \
u v
\
t
are structurally identical.
Next question was find out if 2 binary tree are structurally mirror ?
Any pointer or help are appreciated.
My attempt was
boolean isStrucutrallyIdentitical(BinaryNode root1, BinayNode root2) {
if(root1==null && root2==null) return true;
if(root1==null || root2==null) return false;
if(root1!=null && roo2!=null) return true; // instead of value just check if its null or not
return isStrucutrallyIdentitical(root1.getLeft(), root2.getLeft()) && isStrucutrallyIdentitical(root1.getRight(), root2.getRight());
}
public boolean areStructuralySame(TreeNode<Integer> tree1, TreeNode<Integer> tree2) {
if(tree1 == null && tree2 == null) {
return true;
}
if(tree1 == null || tree2 == null) {
return false;
} else return (areStructuralySame(tree1.getLeft(), tree2.getLeft()) && areStructuralySame(tree1.getRight(), tree2.getRight()));
}
This works fine
private static boolean compare(TNode curRoot, TNode newRoot) {
if (curRoot == null && newRoot == null) {
return true;
} else if ((curRoot == null && newRoot != null) || (curRoot != null && newRoot == null))
return false;
else {
if (compare(curRoot.left, newRoot.left) && compare(curRoot.right, newRoot.right))
return true;
return false;
}
}
In latest version of LINQ to Twitter v2.1.08 there is no longer support for paging. How do I get a certain page without page property?
Cheers
int curPageIndex=5;
string pageindex="5";
string cmd = "next";
using (var twitterCtx = new TwitterContext(myauth))
{
try
{
// set up the "main query"
IQueryable<Status> test = from tweet in twitterCtx.Status select tweet;
switch (cmd)
{
case "prev":
test = pageindex.Length > 0
? test.Where(p => p.Type == StatusType.Home && p.Page == curPageIndex)
: test.Where(p => p.Type == StatusType.Home);
break;
case "next":
test = pageindex.Length > 0
? test.Where(p => p.Type == StatusType.Home && p.Page == curPageIndex)
: test.Where(p => p.Type == StatusType.Home);
break;
default:
//
test = test.Where(p => p.Type == StatusType.Home);
break;
}
Solution: Changed Page parameter to SinceID and MaxID
//Get the statusids in the query, add or subtract so you skip current id's
ulMaxId = test.Min(status => ulong.Parse(status.StatusID)) - 1;
ulSinceID = test.Max(status => ulong.Parse(status.StatusID)) + 1;
//Return ID above and use them in future calls (below)
//Now you can navigate timelines, depending if you are stepping forward or backwards
? test.Where(p => p.Type == StatusType.Home && p.SinceID == ulong.Parse(sinceid)
...
? test.Where(p => p.Type == StatusType.Home && p.MaxID == ulong.Parse(maxid))
i am building a site with a login page and want to prevent user from login after 5 try fails for 20 min
maybe one way is this
const int maxTryCount = 5;
const int minutesToSuspend = 20;
[HttpPost]
public ActionResult Index(PeopleAccount account, string returnUrl)
{
if (Session["TimeStamp"] == null || ((DateTime)Session["TimeStamp"]).AddMinutes(minutesToSuspend) <= DateTime.Now)
{
PeopleAccountService service = new PeopleAccountService();
DataSet ds = service.Authentication(account.UserName, account.Password, null, null);
if (ds.Tables[1].Rows.Count > 0)
{
FormsAuthentication.SetAuthCookie(account.UserName, false);
Session.Clear();
Session["UserAccressibility"] = ds.Tables[0];
Session["UserFullName"] = ds.Tables[1].Rows[0][0];
if (returnUrl != null && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
return Redirect(returnUrl);
return RedirectToAction("Index", "Stuff", null);
}
else
{
Session["TryCount"] = (Session["TryCount"] == null
|| (Session["TimeStamp"] != null && ((DateTime)Session["TimeStamp"]).AddMinutes(minutesToSuspend) <= DateTime.Now)) ?
1 : ((int)Session["TryCount"]) + 1;
if ((int)Session["TryCount"] > maxTryCount)
{
Session["TimeStamp"] = DateTime.Now;
return Redirect("~/UnauthorizedAccess/Index");
}
ModelState.AddModelError("", Paymankaran.Content.Messages.InvalidUsernameAndOrPassword);
ModelState.AddModelError("", string.Format(Paymankaran.Content.Messages.TryCountWarning,
Session["TryCount"], maxTryCount, minutesToSuspend));
return View();
}
}
return Redirect("~/UnauthorizedAccess/Index");
}
}
in fact i am using Session["TimeStamp"] and Session["TryCount"] variables to implement this functionality
is there a better or more secure way?
is this a good way to achieve this functionality?
Using Asp.net membership provider would give you this feature (and much more) for free.