lundi 14 octobre 2019

How to properly add items to database and separation of concerns

I'm trying to create a post to add an entity to my database. I fear that I might be mixing up data and view layers. I'm extremely unfamiliar with design patterns and the repository pattern is killing me.

This is a bit of code from my controller.

[HttpPost("CreateArtItem/{createArtItemRequest}")]
        public async Task<ActionResult<ArtItem>> Put(CreateArtItemRequest createArtItemRequest)
        {
            try
            {
                if (createArtItemRequest != null)
                {
                    ArtItem artItem = new ArtItem()
                    {
                        //mapping
                    };

                    _artRepository.AddArtItem(artItem);
                    _artRepository.Add(artItem);
                    if (await _artRepository.SaveChangesAsync())
                    {
                        return Created(artItem.Id.ToString(), artItem);
                    }
                    else
                    {
                        return this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure.");
                    }
                }
                else
                {
                    return BadRequest();
                }

            }
            catch (Exception e)
            {
                return this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure.");
            }
        }

I'd highly appreciate it if someone could explain to me how the different layers work, what the data operations are, and if this is a proper implementation of post.

I'm following a guide on pluralsight and the dude teaching is doing something similar.

pluralsight

Aucun commentaire:

Enregistrer un commentaire