Home > Net >  How should I pass the author of the change into PUT/POST methods?
How should I pass the author of the change into PUT/POST methods?

Time:01-10

I have a Category class which looks like this:

public class Category
{
    [Required]
    public Guid Id { get; set; }
    [Required]
    public string CategoryName { get; set; }
    public string CategoryDescription { get; set; }
    public DateTime CreatedDate { get; set; }
    public bool isDeleted { get; set; } = false;
    public virtual ICollection<UpdateInfo> Updates { get; set; } = new List<UpdateInfo>();
}

And a UpdateInfo class which looks like this (with enum):

public enum Status
{
    Created,
    Changed,
    Deleted
}
public class UpdateInfo
{
    public Guid Id { get; set; }
    public string Author { get; set; }
    public DateTime Date { get; set; }
    public Status Status { get; set; }
    public virtual Category Category { get; set; }
}

I am looking for a proper way to pass the author into PUT/POST/DELETE methods together with Id or CategoryDTO passed from body. I tried it with POST method first, and I need an opinion, and the proper way to do this.

[HttpPost]
    [ProducesResponseType(StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    public async Task<ActionResult<Category>> PostCategory(CategoryDTO categoryDTO, string author)
    {
        _logger.LogInformation("Creating a new category DB object from a DTO objcect passed in PostOperation method.");
        var category = _mapper.Map<CategoryDTO, Category>(categoryDTO);

        if (CategoryRepeat(category.CategoryName))
        {
            return Conflict("Such category already exists.");
        }

        _logger.LogInformation("Adding an initial update status.");
        var initialUpdate = new UpdateInfo { Author = author, Status = Status.Created, Date = DateTime.UtcNow, Id = Guid.NewGuid(), Category = category };
        category.Updates.Add(initialUpdate);

        try
        {
            _logger.LogInformation($"Trying to save created category {category.Id} into the Database.");
            _context.Categories.Add(category);
            await _context.SaveChangesAsync();
            _logger.LogInformation($"Created category id: {category.Id} saved into the Database successfully.");
        }
        catch (DbUpdateConcurrencyException ex)
        {
            _logger.LogError(ex.Message);
            return BadRequest(new { message = ex.Message });
        }
        return CreatedAtAction("GetCategory", new { id = category.Id }, category);
    }

CodePudding user response:

The way you did it by passing it as a separate variable might be more flexible if you need to pass the author in some requests but not others. So you did good. If you see yourself that you are passing the author everywhere, it would be better to change the CategoryDTO model instead.

Another way would be to make another DTO for the specific action you are looking for. For example CreateCategoryDTO for "create" POST requests and change the model there instead. It really depends on your coding style and the whole "coding standard" of your project.

CodePudding user response:

If I understood your problem correctly, you could also pass the author in the request header. you can create a custom header and set it when sending the request then you can get the info like string author = Request.Headers["Header-Author"];

another way is to add the property to the existing CategoryDTO itself. then get it like string author = categoryDTO.Author;

one way is as you have done by passing as a separate parameter.

  • Related