I have two entities BlogPost and user im not sure how the follow mechanism works I have searched on S.O but could not get an idea
What I think
I think follow is some sort of Get posts where userID = ?
but also it should be a continues process ie when user posts again it should automatically get the new post, until user unfollows.
I would like some suggestions on how to go about implementing and what i will need.
What i am trying to create
I want to create a service that subcribes to a user (like in the social networks such linkedIn, so a user can follow another user there by getting all the users new activities such as new posts (new uploads)
public void subscribeToUser(String id){
//follow a user by id
//im not sure the relationship i should create between the entities
// user and posts so that when one user creates a post
// another user can subscribe to the first user and can see all the posts and future posts of the user
}
}
Related
I'm discovering the Repository Pattern for my Laravel project but I have to say that I'm a bit lost once a model has several dependencies and the examples on the web are always basic and don't answer the question for more complex use cases.
Let's imagine a user on my app. He can have badges, he has different things on the app that will be slightly modified when he first performs the action (when he first sees the comments, I tell him once the different things he can do, etc), he has several "counters" to record the number of comments he made, the number of friends he invited, without having to count each entry each time.
My database looks like this:
users table:
id
pseudo
name
password
...
badges table:
user_id
badge1_xxxxxx
badge2_xxxxxx
...
I have a very limited number of badges so I decided to create a column for each of them and as soon as a user wins a badge, I get his entry (in OneToOne relationship) and I indicate that the badge in question has been won.
onboarding table:
user_id
seen_comments (boolean)
seen_results (boolean)
...
As you can see, I store each action I'd like the user to do in different columns and as soon as he has done one and I've been able to modify my app accordingly (by showing him an arrow, etc), I put the column in question to true.
user_counters table:
user_id
count_comments
count_invited_friends
...
I don't consider a user to be a user if he doesn't have an entry in each of the tables (I could have done everything in one table but the users table seemed to me to become huge). The only relationship used is OneToOne between the user and the table in question.
Should I do this ?
class UserRepository {
public function register($data) {
// Create the user
$user = User::create($data);
// Create all its dependencies which are required if I want to consider the user as fully registered in my DB
$user->badges()->create();
$user->onboarding()->create();
$user->counter()->create();
// Return the user
return $user;
}
}
Or should I create a Repository for each of these elements and create the entire user in a UserService ?
How far should I separate things and when does it become overkill?
Is there something that I don't understand in concept of Repository ? If so, could you give me some links that you found useful because I feel like I ran out of ideas for search keywords.
Thanks
I am new to laravel and do not have much experience with using ORM. I am building a system in which I need build functionality to allow user to switch clients with another user. I am not sure what is the best method to achieve this.
Currently client belongs to user, do I need another association for client and user model using different foreign key ?
public function clients()
{
return $this->hasMany('App\Client');
}
public function user()
{
return $this->belongsTo('App\User');
}
Even though a client can only have one user switch request I would like to track all the requests so for example if user A makes request to switch client Test with user B and then make another request to switch the same client with user C I would like to soft delete the first record and create new record for new request. Once the other user accepts the request we change the primary key for client Test in the clients table. Will this be a One To Many / Many To Many relationship ?
What table naming conventions do I need to follow ? Any help will be much appreciated.
If I understand you correctly, you would like to temporarily switch clients for one user to another. What you should do is have another table say switched-clients that holds records for the switches with columns user1 and user2. When you want to retrieve clients, your controller checks this table first and if the record exists where('user1', 'user A') (according to your example), then you retrieve the clients for user2.
I am doing authentication using azure mobile apps in my xamarin forms app and once I retrieve unique User Authentication Id, I am storing this with bunch of other user information into a sync table called Users as shown below.
So basically I am also using Azure Mobile Sync in my app. Id column below is predefined Azure Mobile Id. Everything works fine as long as I have same user with Id stored in Local sync db.
Once, if I lose this local Db information (It can be caused by reinstalling app or deleting app data). User will prompt to re-login and although I get the same Azure Authentication Id, snyc will cause a new insert into Azure Users table. Because it doesnt know that it is an existing row.
Possible Solutions is to make userAuthId as PK (eventually it is unique). But if I do that I will lose azure mobile sync feature, wont I? Can somebody shade me lights how to make Custom Id column for azure mobile sync? or instead of auto generate, i can pass the value from client.
I encountered the similar issue before, I just set the value of Id column in my users table to UserAuthId. For adding the additional UserAuthId and use it as the foreign key in other tables, I assumed that after user logged, you need to check the online Users table and try to retrieve the existing user record based on the current UserAuthId, then you could insert/update your local data store or directly update your online Users table. Moreover, I would recommend you follow adrian hall's book here about developing Azure Mobile Apps with Xamarin.
I found a solution myself following suggestion from Bruce Chen. Basically just remove the Default Value or Binding as shown in the image and on your backend Api change the Post function as below and also your controller should retrieve UserId from the authentication by calling ClaimsPrincipal as below. Using this method, you get the authenticated UserId within your Api. You can see the full implementation on this github link. So you can pass this as Id of the table. I also added extra check before I insert that if Id is already exist or not since I am not auto generating the Id.
I hope this helps to anybody else having same problem.
public string UserId => ((ClaimsPrincipal)User).FindFirst(ClaimTypes.NameIdentifier).Value;
public async Task<IHttpActionResult> PostUser(User item)
{
try
{
User current;
item.Id = UserId;
myAppsAPIContext db = new myAppsAPIContext();
User User = db.Users.Where(u => u.Id == UserId).SingleOrDefault();
if (User == null)
{
current = await InsertAsync(item);
}
else
current = User;
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
catch (Exception ex)
{
TelemetryClient.TrackException(ex);
return null;
}
}
I am using a Parse.Query to look up a record with javascript.
It works fine if the user making the query is logged in, but if its just a random unlogged in user making the query, it returns an empty set of results []
var userquery = new Parse.Query(Parse.User);
userquery.equalTo("username", "johndoe");
Is there something I need to do to enable random people who hit my webpage to run parse queries?
Also, even if I'm logged in. I can't query for a user besides my own. It also returns []
This describes my same problem, but there is no solution: https://www.parse.com/questions/querying-on-parseuser-object-always-returns-empty-array
The User class is restricted for very good security reasons. It would be quite a security hole if unauthenticated users could query the user table and get even partial login credentials.
If you really need some of the information to be open to unauthenticated users you should put it in another class with no security and link to it from the User class.
I have a tricky issue here with a registration of both a user and his/her pet. Both the user and the pet are treated as separate entities and both require separate registration forms. However, the user's pet has to be linked to the user via a foreign key in the database. The process is basically that when a new user joins the site, firstly they register their pet, then they register themselves. The reason for this order is to check their pet's eligibility for the site (there are some criteria to be met) first, instead of getting the user to sign up only to then find out their pet is ineligible. It is this ordering of the form submissions which is causing me a bit of a headache, as follows...
The site is being developed with an MVC framework, and the User registration process is managed via a method in a User_form controller, while the pet registration process is managed via a method in the Pet_form controller.
The pet registration form happens first, and the pet data can be saved without the owner_id at this stage, with the user id possibly being added (e.g by retrieving pet's id from session) following user registration. However, doing it this way could potentially result in redundant data, where pet records would be created in the database, but if the user doesn't actually register themselves too, then the pets will be ownerless records in the DB.
Other option is to serialize the new pet's data at the pet registration stage, don't save it to the DB until the user fills out their registration form. Once the user is created, i can pass serialised data AND the owner_id to a method in the Pet Model which can update the DB. However, I also need to set the newly created $pet to $this->pet which I then access for a sequence of other related forms. Should I just set the session variable in the model method? Then in the Pet controller constructor, do a check for pet stored in session, if yes, assign to $this->pet...
If this makes any sense to anybody and you have some advice, i'd be grateful to hear it!
Here's a slightly left-field solution (which may or may not work depending on your situtation:
Require the user to enter a valid email address upon pet registration, and then link the user with the pet upon user registration by matching email address (or hash of email address).
If you're left with dangling pet references, you could send an email to the pet owner saying "I'm about to delete your pet" after a month (if there's no associated user id), or something like that.