public async Task <Traveler> CreateAsync(Traveler traveler) { if (traveler.Id != 0) { throw new ArgumentException("The 'Id' must be different from 0."); } traveler = travelerRepository.Add(traveler); await travelerRepository.UnitOfWork.SaveChangesAsync(); return(traveler); }
public HttpResponseMessage Post(Traveler traveler) { if (travelers.FindBy(t => t.TravelerUserIdentity == traveler.TravelerUserIdentity).Any()) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } // saving the new order to the database travelers.Add(traveler); travelers.Save(); // creating the response, the newly saved entity and 201 Created status code var response = Request.CreateResponse(HttpStatusCode.Created, traveler); response.Headers.Location = new Uri(Request.RequestUri, "travelers/" + traveler.TravelerUserIdentity); return(response); }
public async Task <ActionResult <CommentsDto> > Post(CommentsDto dto) { try { var mappedEntity = _mapper.Map <Comments>(dto); _eventRepository.Add(mappedEntity); if (await _eventRepository.Save()) { var location = _linkGenerator.GetPathByAction("Get", "Comments", new { mappedEntity.Id }); return(Created(location, _mapper.Map <CommentsDto>(mappedEntity))); } } catch (Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, ex.InnerException.Message)); } return(BadRequest()); }
public static void Main() { Console.WriteLine("TraPaConsole is starting."); //todo connection string IDataAccessor dataAccessor = new DataAccessor(false, "Data Source=ConsoleRunDatabase.db"); using (var dbContext = dataAccessor.GetNewDatabaseContext()) { //dbContext.Database.EnsureDeleted(); dbContext.Database.EnsureCreated(); //dbContext.Database.Migrate(); } IDatabaseRepositoryFactory databaseRepositoryFactory = new DatabaseRepositoryFactory(dataAccessor); ITravelerRepository travelerRepository = databaseRepositoryFactory.GetNewTravelerRepository(); ITravelDateRepository travelDateRepository = databaseRepositoryFactory.GetNewTravelDateRepository(); ITravelerTravelDateReferenceRepository travelerTravelDateReferenceRepository = databaseRepositoryFactory.GetNewTravelerTravelDateReferenceRepository(); Console.WriteLine("Provide the action you would like to execute \n" + "add : add a new traveler to the database \n" + "list : list all travelers \n" + "delete : remove traveler with a given index \n" + "addtravel : add a new travel to the database \n" + "listtravel : list all travels \n" + "deletetravel : remove travel with a given index \n" + "addreference : add a new travel travel date reference to the database \n" + "listreference : list all travel travel date references \n" + "deletereference : remove travel travel date references with a given index \n" + "q : exit"); var flag = true; do { var input = Console.ReadLine(); var punctuation = input.Where(Char.IsPunctuation).Distinct().ToArray(); var words = input.Split().Select(x => x.Trim(punctuation)).ToList(); switch (words[0]) { case "add": try { var traveler = CreateTraveler(words); traveler = travelerRepository.Add(traveler.FirstName, traveler.LastName, traveler.PhoneNumber, traveler.DirectionFrom, traveler.DirectionTo, traveler.DoesPay, traveler.Price); Console.WriteLine($"Traveler was added. Id: '{traveler.Id}'"); } catch (Exception e) { Console.WriteLine("Could not add the traveler to the database. Reason:"); Console.WriteLine(e); } break; case "list": var travelers = travelerRepository.GetAll(); WriteTravelers(travelers); break; case "delete": try { var idToDelete = Int32.Parse(words[1]); travelerRepository.Remove(idToDelete); Console.WriteLine($"Traveler was removed. Id: {idToDelete}"); } catch (Exception e) { Console.WriteLine("Could not remove the traveler from the database. Reason:"); Console.WriteLine(e); } break; case "addtravel": try { var travel = CreateTravel(words); travelDateRepository.Add(travel.TravelName, travel.DateOfTravel); Console.WriteLine("Travel was added."); } catch (Exception e) { Console.WriteLine("Could not add the travel to the database. Reason:"); Console.WriteLine(e); } break; case "listtravel": var travelDates = travelDateRepository.GetAll(); WriteTravels(travelDates); break; case "deletetravel": try { var idToDelete = Int32.Parse(words[1]); travelDateRepository.Remove(idToDelete); Console.WriteLine($"Travel was removed. Id: {idToDelete}"); } catch (Exception e) { Console.WriteLine("Could not remove the travel from the database. Reason:"); Console.WriteLine(e); } break; case "addreference": try { var travelTravelDateReference = CreateTravelTravelDateReference(words); travelerTravelDateReferenceRepository.Add(travelTravelDateReference.TravelerId, travelTravelDateReference.TravelDateId); Console.WriteLine("Traveler - travel date reference was added."); } catch (Exception e) { Console.WriteLine("Could not add the traveler - travel date reference to the database. Reason:"); Console.WriteLine(e); } break; case "listreference": var travelerTravelDateReferences = travelerTravelDateReferenceRepository.GetAll(); WriteTravelTravelDateReferences(travelerTravelDateReferences); break; case "deletereference": try { var travelerId = Int32.Parse(words[1]); var travelDateId = Int32.Parse(words[2]); travelerTravelDateReferenceRepository.Remove(travelerId, travelDateId); Console.WriteLine("Traveler - travel date reference was removed."); } catch (Exception e) { Console.WriteLine("Could not remove the traveler - travel date reference from the database. Reason:"); Console.WriteLine(e); } break; case "q": flag = false; Console.WriteLine("Stopping the application."); break; default: Console.WriteLine(@"Invalid key: {0}, try again.", words[0]); break; } } while (flag); }