public async Task <BaseResult <DisplayPlatformTicketModel> > BookPlatformTicket(string userId, PlatformTicketModel model) { BaseResult <DisplayPlatformTicketModel> result = new BaseResult <DisplayPlatformTicketModel>(); if (model != null && !String.IsNullOrWhiteSpace(userId)) { try { //Get user var user = await _userRepository.GetUser(userId); if (user != null) { PlatformTicketDataModel dataModel = new PlatformTicketDataModel(); dataModel.InjectFrom(model); dataModel.TicketStatus = TicketStatus.Booked; dataModel.Id = Guid.NewGuid().ToString(); dataModel.UserId = user.Id; //Insert data to mongo var platformTicketCollection = _database.GetCollection <PlatformTicketDataModel>("cPTickets"); await platformTicketCollection.InsertOneAsync(dataModel); var filter = Builders <PlatformTicketDataModel> .Filter.Eq("Id", dataModel.Id); var bookedTicket = await platformTicketCollection.Find(filter).ToListAsync(); DisplayPlatformTicketModel displaymodel = null; if (bookedTicket != null && bookedTicket.Count == 1) { displaymodel = Convert(bookedTicket.FirstOrDefault()); } result.Suceeded = true; result.Value = displaymodel; } else { result.Suceeded = false; result.AddError("User is unauthenticated."); } } catch (Exception) { result.Suceeded = false; result.AddError("Error while booking platformticket, please try after some time."); } } else { result.Suceeded = false; result.SetRequiredFieldsMissing("Fields missing"); } return(result); }
private DisplayPlatformTicketModel Convert(PlatformTicketDataModel dataModel) { DisplayPlatformTicketModel model = null; if (dataModel != null) { model = new DisplayPlatformTicketModel(); model.InjectFrom(dataModel); model.TicketStatus = dataModel.TicketStatus; } return(model); }