Where and when should the coinbase transaction(including the miner key) be added?

I’m making a simple demonstrator program, pretty much a simplified bitcoin clone based mostly on the original whitepaper. Please bear with me, because I haven’t found a way to describe the problem more concisely. My blockchain is based on this tutorial. However, it completely ignores the transaction part, so I’ve based the transactions on this…