Today let’s put another design to your Online API – the brand new Weapon . The theory about this is certainly that each and every Reputation will has you to definitely Firearm and you may vice versa.
I am aware, that isn’t your own normal character-to experience video game. Always, you’d possess numerous firearms in your collection and make it easy for you to otherwise a few, but for this example, your own RPG character only will get that firearm if in case you want to alter they, you have to drop additional.
Then, we need to incorporate other possessions towards Reputation classification, particularly the Gun . Can you already find out how to create a-one-to-that loved ones?
Because i additional an alternative model i also want to see it as a table on the database. Very, we have to range from the Weapon category just like the DbSet on DataContext .
Okay, it looks like we already had everything you in a position with the migration. Therefore, on critical i type dotnet ef migrations add Gun and hit go back.
The child/mainly based side couldn’t become calculated to the one to-to-one dating ranging from ‘Character.Weapon’ and you can ‘Weapon.Character’. To understand the child/depending section of the relationship, arrange the fresh new foreign secret possessions. If this type of navigations really should not be an element of the exact same matchmaking arrange them in place of specifying new inverse.
Ok. So, this is why we have to explain a specific overseas key property. With the aid of the fresh overseas key, the databases understands where to look toward related entity.
Inside our example, we want the new Weapon to be new built side of the matchmaking. There should be a character for this Weapon . It cannot exist without one. The smoothness likewise can be exists as opposed to a gun .
We understand one to by adding various other possessions for the Gun category, which would be the possessions CharacterId given that an enthusiastic int . By using that summit – by using the C# classification name in addition to text “Id” – Entity Design knows that this is basically the involved international key getting the type property. That’s all currently.
On the Upwards() types of the fresh migration file, we come across producing the new Firearms desk as well as the concept of new foreign secret restriction.
Regarding migration framework file, we in addition to understand the concept of the family relations ranging from Character and you may Firearm . On the procedures HasOne() and WithOne() Entity Structure defines the only-to-you to definitely family. Concurrently, you will find the phrase the overseas trick and get what the results are if the a character gets eliminated. Which have OnDelete(DeleteBehavior.Cascade) the fresh new Weapon would-be eliminated making use of the Profile .
Put Weapons to RPG Characters
We begin by carrying out another folder for the WeaponService and you will upcoming produce the interface IWeaponService . It screen just gets one method entitled AddWeapon() , coming back a beneficial GetCharacterDto and you will delivering an AddWeaponDto .
For that, we perform a separate Firearm folder in the Dtos folder and you can produce the C# classification AddWeaponDto into characteristics Name , Wreck and you can CharacterId .
Next, we create the WeaponService execution group and apply the fresh relevant screen and you will are the async keyword to your AddWeapon() means.
We also need an effective constructor injecting brand new DataContext , the fresh IHttpContextAccessor to obtain the already licensed affiliate and you can a keen IMapper . We want the consumer to confirm the fresh new considering CharacterId after.
State-of-the-art Dating that have Organization Build Core (continued)
Regarding the implementation Indianapolis IN sugar babies of this new AddWeapon() method we first initialize the fresh ServiceResponse , come back this response and already would a try/hook take off in which we place ideal philosophy in the event regarding a different.
Now let’s talk about the fresh new try block, we first obtain the best Character regarding databases. We availableness the brand new Letters about _context , discover the very first entity to the offered CharacterId while the proper Affiliate to make certain that we all know it reputation very falls under the currently signed up affiliate.
Simply to review, we have new Id of your own most recent user by the accessing the newest NameIdentifier states well worth regarding JSON web token.
not, whenever we got the best profile , we are able to would yet another Firearm such as, for the considering Label and Destroy worthy of and have set the latest Character assets associated with the this new Firearm instance with the reputation target i had about database.
By-the-way, we could features additional a separate mapping regarding AddWeaponDto so you’re able to the Firearm type, otherwise we simply place these features by hand right here.
It derives regarding ControllerBase so we have to are the characteristics [Route(“[controller]”)] , [ApiController] and [Authorize] aided by the relevant playing with directives.
Following we want good constructor that is injecting the new IWeaponService . Of course we are able to initialize so it career on the factor.
After that, we only need one Article approach, that’s AddWeapon() with an enthusiastic AddWeaponDto just like the a factor and returning the result of new AddWeapon() sorts of the fresh new _weaponService .
Upcoming we try to help make an alternate firearm with the Hyperlink , this new HTTP method is Article and don’t forget brand new Agreement secret from the header with the best holder token.
Regarding your looks, definitely use a cool name to suit your weapon and you will the correct characterId for your signed-inside associate.
Powering the decision contributes to a successful effect. But it is not too fulfilling. We don’t understand the weapon of the profile. Really, naturally, we do not view it, as i haven’t added the firearm on GetCharacterDto .
We obtain a difference! Not too helpful. Nevertheless when we have a closer look at Terminal into the Visual Facility Code, we could comprehend the adopting the mistake message.
Also it actually informs us the latest Id of one’s profile. So the that-to-one to family members performs just fine. To add another firearm, we first need certainly to get rid of the Learn Sword regarding the databases dining table.
I would an alternative GetWeaponDto class you to simply include the fresh Identity additionally the Ruin of one’s Weapon . It’s not necessary to own an enthusiastic Id or the Reputation here.
That’s all. When we now erase the fresh gun of your character on the databases earliest then do they once more that have Postman, we obtain the whole profile back – towards the associated weapon.