public bool TryGetTimelines(int tourId, out List <TimeLine> timelines) { try { DbService.ConnectDb(out _context); var tour = _context.Tours.SingleOrDefault(t => t.Id == tourId && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Tour not found"); timelines = _context.TimeLines.Where(t => t.TourId == tourId && t.DeletedAt == null)?.ToList() ?? new List <TimeLine>(); foreach (var timeLine in timelines) { var timelineDetails = _context.TimelineDetails.Where(td => td.TimelineId == timeLine.Id && td.DeletedAt == null); timeLine.TimelineDetails ??= new List <TimelineDetail>(); timeLine.TimelineDetails.AddRange(timelineDetails); } _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool Save(T item) { try { if (Exist(item.Id)) { item.DataAlteracao = DateTime.Now; _context.Entry(item).State = EntityState.Modified; } else { item.DataCriacao = DateTime.Now; item.DataAlteracao = DateTime.Now; _context.Set <T>().Add(item); } } catch (Exception ex) { if (ex.InnerException == null) { _listaViolacacao.Add(ex.InnerException.Message); } else { _listaViolacacao.Add(ex.Message); } return(false); } bool ok = _context.SaveChanges() > 0; _context.SaveChanges(); return(ok); }
public TEntity Add(TEntity obj) { var entity = db.Set <TEntity>().Add(obj); db.SaveChanges(); return(entity); }
public bool RevokeToken(string userName) { var user = _context.Users.SingleOrDefault(u => (u.UserName == userName)); if (user is null) { return(false); } user.RefreshToken = null; _context.SaveChanges(); return(true); }
public T Create(T item) { try { dataset.Add(item); _context.SaveChanges(); return(item); } catch (Exception) { throw; } }
// Metodo responsável por criar uma nova pessoa // Se tivéssemos um banco de dados esse seria o // momento de persistir os dados public Person Create(Person person) { try { // Adiciona o objeto 'person' no banco e salva as alterações _context.Add(person); _context.SaveChanges(); } catch (Exception ex) { throw ex; } return(person); }
public T Create(T item) { try { // Adiciona o objeto 'person' no banco e salva as alterações dataset.Add(item); _context.SaveChanges(); } catch (Exception ex) { throw ex; } return(item); }
public bool TryUpdatePlace(Place place) { try { DbService.ConnectDb(out _context); _context.Places.Update(place); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool AddNewPost(Post post) { try { DbService.ConnectDb(out _context); if (post.TourId != null) { var tour = _context.Tours.FirstOrDefault(t => t.Id == post.TourId && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Tour not found"); var tourInfo = _context.TourInfos.FirstOrDefault(ti => ti.Id == tour.TourInfoId) ?? throw new ExceptionWithMessage("Tour info not found"); tourInfo.Rating ??= 0; tourInfo.Rating = (tourInfo.TotalRating * tourInfo.Rating + post.Rating) / (tourInfo.TotalRating + 1); tourInfo.TotalRating++; } else { post.Rating = null; } _context.Posts.Add(post); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryAddService(Service service, int createByUserId) { do { try { DbService.ConnectDb(out _context); _context.Services.Add(service); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } } while (false); return(true); }
public ErrorCode TryAddFriend(int userId, int userRequestId) { ErrorCode errorCode; do { TryGetFriendRequest(userId, userRequestId, out var friendDb); DbService.ConnectDb(out _context); if (friendDb != null) { if (friendDb.AcceptedAt != null) { errorCode = ErrorCode.FriendRequestAlreadySent; break; } errorCode = ErrorCode.AlreadyFriend; break; } var friend = new Friend(userId, userRequestId); try { DbService.ConnectDb(out _context); _context.Friends.Add(friend); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } errorCode = ErrorCode.Success; } while (false); return(errorCode); }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new PostgreSQLContext( serviceProvider.GetRequiredService < DbContextOptions <PostgreSQLContext> >())) { // Look for any movies. if (context.Movies.Any()) { return; // DB has been seeded } context.Movies.AddRange( new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-2-12"), Genre = "Romantic Comedy", Price = 7.99M, Rating = "普" }, new Movie { Title = "Ghostbusters ", ReleaseDate = DateTime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M, Rating = "普" }, new Movie { Title = "Ghostbusters 2", ReleaseDate = DateTime.Parse("1986-2-23"), Genre = "Comedy", Price = 9.99M, Rating = "普" }, new Movie { Title = "Rio Bravo", ReleaseDate = DateTime.Parse("1959-4-15"), Genre = "Western", Price = 3.99M, Rating = "普" } ); context.SaveChanges(); } }
public bool TryAddTour(Tour tour, List <TimeLine> timeLines) { try { DbService.ConnectDb(out _context); // Store tour _context.Tours.Add(tour); _context.SaveChanges(); // Store Timelines foreach (var timeline in timeLines) { timeline.TourId = tour.Id; _context.TimeLines.Add(timeline); _context.SaveChanges(); if (timeline.TimelineDetails != null) { foreach (var timelineDetail in timeline.TimelineDetails) { var place = _context.Places.FirstOrDefault(p => p.Id == timelineDetail.PlaceId); // Check place is exist if (place == null) { continue; } // Store time line detail timelineDetail.TimelineId = timeline.Id; _context.TimelineDetails.Add(timelineDetail); _context.SaveChanges(); } } } } finally { DbService.DisconnectDb(ref _context); } return(true); }
//没有共通化 public int SaveSalonService(string salonCd, ObservableCollection <ServiceModel> models) { using (var context = new PostgreSQLContext()) { using (var transaction = context.Database.BeginTransaction()) { try { int res = 0; var salonServices = context.SalonServices.Where(s => s.SalonCode == salonCd).Include(s => s.Service).ToList(); var data = salonServices.Where(w => !models.Select(s => s.Code).Contains(w.Service.Code)); context.SalonServices.RemoveRange(data); foreach (ServiceModel model in models) { if (salonServices.Where(w => w.Service.Code == model.Code).Count() < 1) { DBModels.PostgreSQL.SalonService salon = new DBModels.PostgreSQL.SalonService(); DBModels.PostgreSQL.Service service = context.Services.Where(s => s.Code == model.Code).First(); salon.SalonCode = salonCd; salon.ServiceCode = model.Code; salon.Service = service; context.SalonServices.Add(salon); } } res += context.SaveChanges(); transaction.Commit(); return(res); } catch (Exception ex) { transaction.Rollback(); AppLog.Error(typeof(SalonControl), ex.ToString()); throw new AppException("DBException"); } } } }
public bool TryAddUser(User user) { try { DbService.ConnectDb(out _context); _context.Users.Add(user); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }