Hi. Let me clear it out 😀
German cases have a base function based on the question they answer to so to say, and the other one based on the preposition.
Nominative = Who/What does the action?
Accusative = Who/What receives the action?
Dative = To who / To what goes the action?
Genitive = Of who / Of what?
That's the base function but depending on the preposition, the case will change. Most prepositions requires the dative, some the accusative and a few the genitive (you have to memorize them). Some, though, especially the ones that describe a movement and / or a position, can be both an accusative or dative depending on their role in the sentence, namely depending on whether we are taking about a movement or a position.
In + Position = Dative
In + Movement = Accusative
I hope it helped.