kika123
kika123

Reputation: 31

How to retain data gained from multiple requests in EF Core?

I have asp.net core web api with React client. I'm adding data through my user interface created in React. In my api, Db context is added as scoped service, and each time my request finishes and new one is started, all my data from previous request is lost. This is how my Configure services looks like:

 services.AddDbContext<TicketingContext>(o=>o.UseLazyLoadingProxies().UseSqlServer(connectionString));

Controller method for posting data looks like this:

 [HttpPost("{id}/tickets")]
    public IActionResult CreateNewTicket(string id,
        [FromBody] TicketForCreationDto ticketForCreation)
    {
        if (ticketForCreation == null)
        {
            return BadRequest();
        }

        var ticketEntity = _mapper.Map<Ticket>(ticketForCreation);
        _ticketRepository.AddNewTicket(ticketEntity);
        _ticketRepository.AddTicketToClient(id, ticketEntity);
        if (!_ticketRepository.Save())
        {
            throw new Exception("Creating ticket failed on save");
        }

        var ticketToReturn = _mapper.Map<TicketDto>(ticketEntity);



        return CreatedAtRoute("GetTicket", new {id=id, ticketId = ticketToReturn.Id }, ticketToReturn);
    }

and methods in repository like this: AddNewTicket:

  public void AddNewTicket(Ticket ticket)
    {
        if (ticket.Id == Guid.Empty)
        {
            ticket.Id = Guid.NewGuid();
        }

        var dispatcher = AssignTicketToDispatcher(ticket);
        if (dispatcher == null)
        {
            throw new Exception("There are no dispatchers matching this ticket");
        }

        dispatcher.UserTickets.Add(new UserTicket()
        {
            IdentityUser = dispatcher,
            Ticket = ticket,
            UserId = dispatcher.Id,
            TicketId = ticket.Id
        });


        _context.Tickets.Add(ticket);
    }

AddTicketToClient:

  public void AddTicketToClient(string id, Ticket ticket)
    {
        var client = _identityUserRepository.GetClient(id);
        if (client == null)
        {
            client = _context.Users.Where(u => u.UserName == "username").FirstOrDefault();
        }
        client.UserTickets.Add(new UserTicket()
        {
            IdentityUser = client,
            Ticket = ticket,
            UserId = client.Id,
            TicketId = ticket.Id
        });
    }

Save:

 public bool Save()
    {
        return (_context.SaveChanges() >= 0);
    }

I want to be able to store data gained through multiple requests. Does anyone have idea how to do that?

Upvotes: 0

Views: 680

Answers (1)

JMP
JMP

Reputation: 1934

Use the database as it's the best method you have for persisting your data.

So When you do a request - at the end of the request, after your latest data is saved - query for the data from previous requests that you need and return it.

e.g. retrieve the last 5 requests saved newest first (where id is example for your primary key field):

var latestSaved = _context.UserTickets.OrderByDescending(x => x.id).Take(5);

Or amend to return all relevant data for e.g. active user by passing a user id stored client side.

Pass through any params you need to request the relevant data. Use joins / includes set up in your entities. Whatever you need to do - make use of your entity relationships to get what you need from you database. Why try and replicate what it already does? :)

Upvotes: 1

Related Questions