public async Task <EventDto> AddEvents(EventDto model) { try { Event mappedEvent = _mapper.Map <Event>(model); _generalPersistence.Add <Event>(mappedEvent); if (await _generalPersistence.SaveChangesAsync()) { //The eventDto is returned just in case the dev wants to use it for something in project. Event unmappedEvent = await _eventPersistence.GetEventByIdAsync(mappedEvent.Id); EventDto mappedEventDto = _mapper.Map <EventDto>(unmappedEvent); return(mappedEventDto); } return(null); } catch (Exception ex) { throw new Exception($"Erro: {ex.Message}"); } }
public async Task <BatchDto[]> SaveBatch(int eventId, BatchDto[] models) { try { //Takes all batches from a given Event Batch[] batches = await _batchPersistence.GetAllBatchesByEventIdAsync(eventId); if (batches == null) { return(null); } foreach (BatchDto model in models) { if (model.Id < 1) { Batch batchToAdd = _mapper.Map <Batch>(model); batchToAdd.EventId = eventId; _generalPersistence.Add <Batch>(batchToAdd); await _generalPersistence.SaveChangesAsync(); } //If a batch (model) has an Id, it means that it will be updated, because already exists. else { //Takes the each batch from a given Event. Batch batch = batches.FirstOrDefault(b => b.Id == model.Id); model.EventId = eventId; //Pass to the batch the new values contained in the model. _mapper.Map(model, batch); //Updates it. _generalPersistence.Update <Batch>(batch); await _generalPersistence.SaveChangesAsync(); } } var batchReturn = await _batchPersistence.GetAllBatchesByEventIdAsync(eventId); return(_mapper.Map <BatchDto[]>(batchReturn)); } catch (Exception ex) { throw new Exception(ex.Message); } }