Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a custom entity #407

Open
JornWildt opened this issue Mar 14, 2021 · 2 comments
Open

How to create a custom entity #407

JornWildt opened this issue Mar 14, 2021 · 2 comments
Milestone

Comments

@JornWildt
Copy link

Sorry, but I'm lost in the documentation. I can find examples of defining custom entities and querying custom entities and it works for me - I can create bookings in the admin interface and list them in a separate page.

I can also finde examples of commands that create custom data (not custom entities) like CatLike in the SPA example - but how do I add an instance of a custom entity to the database programmatically? Could you please clarify that in the docs?

I have a booking data model:

public class BookingDataModel : ICustomEntityDataModel
{
  public DateTime? ArrivalDate { get; set; }

  ... more properties ...
}

I also have a web-form for submitting a booking request. I can also make a command as well as a command handler. But what should I do in the command handler?

public class AddBookingCommandHandler : ICommandHandler<AddBookingCommand>
{
  private readonly CofoundryDbContext _dbContext;

  public AddBookingCommandHandler(CofoundryDbContext dbContext)
  {
    _dbContext = dbContext;
  }

  public async Task ExecuteAsync(AddBookingCommand command, IExecutionContext executionContext)
  {
    var booking = new BookingDataModel();

    // populate booking data model with input from command

    // *** THIS IS WRONG ***
    // Booking data model implements ICustomEntityDataModel - but it is not a CustomEntity in itself.
    await _dbContext.CustomEntities.AddAsync(booking);

    // SO ... how to add the booking data model?

    await _dbContext.SaveChangesAsync();
  }
}
@JornWildt
Copy link
Author

Nothing like writing down your issue that makes you figure out your problems all by yourself.

I realized that I needed to issue an AddCustomEntityCommand and then I had a place to pass my BookingDataModel (ICustomEntity):

  var myBooking = new BookingDataModel { ... }

  using (var scope = ContentRepository.Transactions().CreateScope())
  {
    var addCustomEntityCommand = new AddCustomEntityCommand
    {
      CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
      Model = myBooking,
      UrlSlug = "Booking",
      Title = "Booking" + request.Booking.ArrivalDate?.ToString("d"),
      Publish = true,
      PublishDate = DateTime.Now
    };

    await ContentRepository.ExecuteCommandAsync(addCustomEntityCommand);
    await scope.CompleteAsync();
  }

@JornWildt
Copy link
Author

May I suggest that you add that knowledge to the online documentation?

@HeyJoel HeyJoel added this to the Backlog milestone Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants