public ServiceResultModel <List <HotelDefinitionVM> > GetHotels(HotelFilter filter) { List <HotelDefinitionVM> resultList = new List <HotelDefinitionVM>(); using (EFBookingContext context = new EFBookingContext()) { IQueryable <HotelDefinition> hotelDefinitionList = context.HotelDefinitions; if (filter.HotelName.IsNotNull()) { hotelDefinitionList = hotelDefinitionList.Where(p => p.Title.Contains(filter.HotelName)); } if (filter.HotelTypeId > 0) { hotelDefinitionList = hotelDefinitionList.Where(p => p.HotelTypeId == filter.HotelTypeId); } hotelDefinitionList.ToList().ForEach(p => { resultList.Add(p.MapProperties <HotelDefinitionVM>()); }); return(ServiceResultModel <List <HotelDefinitionVM> > .OK(resultList)); } }
public ServiceResultModel <AttributeVM> UpdateAttribute(AttributeVM model) { using (EFBookingContext context = new EFBookingContext()) { var currentItem = context.Attributes.FirstOrDefault(p => p.Id == model.Id); if (currentItem != null) { if (context.Attributes.Any(p => p.Id != model.Id && (p.Name.Equals(model.Name) && p.AttributeType == model.AttributeType))) { return(new ServiceResultModel <AttributeVM> { Code = ServiceResultCode.Duplicate, Data = currentItem.MapToViewModel <AttributeVM>(), ResultType = OperationResultType.Warn, Message = "This title using other records " }); } currentItem.Name = model.Name; currentItem.Description = model.Description; context.Entry <Attributes>(currentItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } return(ServiceResultModel <AttributeVM> .OK(currentItem.MapToViewModel <AttributeVM>())); } }
public ServiceResultModel <List <HotelTypeVM> > GetAllHotelTypes(HotelTypeFilter filter) { List <HotelTypeVM> resultList = new List <HotelTypeVM>(); using (EFBookingContext context = new EFBookingContext()) { IQueryable <HotelType> hotelTypeList = context.HotelTypes; if (filter.Title.IsNotNull()) { hotelTypeList = hotelTypeList.Where(p => p.Title.Contains(filter.Title)); } if (filter.IsActive.HasValue && filter.IsActive.Value) { hotelTypeList = hotelTypeList.Where(p => p.IsActive == filter.IsActive); } hotelTypeList.ToList().ForEach(p => { resultList.Add(p.MapToViewModel <HotelTypeVM>()); }); return(ServiceResultModel <List <HotelTypeVM> > .OK(resultList)); } }
public ServiceResultModel <HotelVM> UpdateHotel(HotelVM model) { using (EFBookingContext context = new EFBookingContext()) { var currentItem = context.Hotels.FirstOrDefault(p => p.Id == model.Id); if (currentItem != null) { if (context.Hotels.Any(p => p.Id != model.Id && p.Title.Equals(model.Title))) { return(new ServiceResultModel <HotelVM> { Code = ServiceResultCode.Duplicate, Data = currentItem.MapProperties <HotelVM>(), ResultType = OperationResultType.Warn, Message = "This title using other records " }); } currentItem.Title = model.Title; currentItem.HotelTypeId = model.HotelTypeId; context.Entry <Hotel>(currentItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } return(ServiceResultModel <HotelVM> .OK(currentItem.MapProperties <HotelVM>())); } }
public ServiceResultModel <UserVM> RegisterUser(RegisterVM model) { using (EFBookingContext context = new EFBookingContext()) { var userInfo = context.Users.FirstOrDefault(p => p.EMail == model.EMail); if (userInfo != null) { return(new ServiceResultModel <UserVM> { Code = ServiceResultCode.Duplicate, Message = "Duplicate User", ResultType = OperationResultType.Warn }); } User registeredUser = context.Users.Add(new User { Name = model.Name, LastName = model.LastName, Deleted = false, EMail = model.EMail, EmailConfirmation = false, IsActive = true, Password = model.Password, TokenId = Guid.NewGuid().ToString() }); context.SaveChanges(); return(ServiceResultModel <UserVM> .OK(registeredUser.MapProperties <UserVM>())); } }
public ServiceResultModel <RoomTypeVM> UpdateRoomType(RoomTypeVM model) { using (EFBookingContext context = new EFBookingContext()) { var currentItem = context.RoomTypes.FirstOrDefault(p => p.Id == model.Id); if (currentItem != null) { // mevcut kayıt haricinde title ile aynı kayıt olamaz kontrol ediyoruz if (context.RoomTypes.Any(p => p.Id != model.Id && p.Title.Equals(model.Title))) { return(new ServiceResultModel <RoomTypeVM> { Code = ServiceResultCode.Duplicate, Data = currentItem.MapToViewModel <RoomTypeVM>(), ResultType = OperationResultType.Warn, Message = "This title using other records " }); } currentItem.Title = model.Title; currentItem.Description = model.Description; context.Entry <RoomType>(currentItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } return(ServiceResultModel <RoomTypeVM> .OK(currentItem.MapToViewModel <RoomTypeVM>())); } }
public ServiceResultModel <AuditLogVM> SaveAuditLog(AuditLogVM auditLog) { using (EFBookingContext context = new EFBookingContext()) { context.AuditLogs.Add(auditLog.MapProperties <AuditLog>()); context.SaveChanges(); } return(ServiceResultModel <AuditLogVM> .OK(auditLog)); }
public ServiceResultModel <RoomTypeVM> DeleteHotelType(int id) { using (EFBookingContext context = new EFBookingContext()) { var deleteItem = context.RoomTypes.FirstOrDefault(p => p.Id == id); context.RoomTypes.Remove(deleteItem); context.SaveChanges(); return(ServiceResultModel <RoomTypeVM> .OK(deleteItem.MapToViewModel <RoomTypeVM>())); } }
public ServiceResultModel <List <AuditLogVM> > GetAuditLogs() { List <AuditLogVM> resultList = new List <AuditLogVM>(); using (EFBookingContext context = new EFBookingContext()) { var auditLogs = context.AuditLogs.ToList(); resultList.AddRange(auditLogs.Select(p => p.MapProperties <AuditLogVM>())); } return(ServiceResultModel <List <AuditLogVM> > .OK(resultList)); }
public ServiceResultModel <List <DistrictDefinitionVM> > GetDistrictByCityId(int cityId) { List <DistrictDefinitionVM> resultList = new List <DistrictDefinitionVM>(); using (EFBookingContext context = new EFBookingContext()) { var districts = context.DistrictDefinition.Where(p => p.CityId == cityId).ToList(); resultList.AddRange(districts.Select(p => p.MapProperties <DistrictDefinitionVM>())); } return(ServiceResultModel <List <DistrictDefinitionVM> > .OK(resultList)); }
public ServiceResultModel <HotelTypeVM> GetHotelType(int id) { if (id <= 0) { return(null); } HotelTypeVM currentItem = null; using (EFBookingContext context = new EFBookingContext()) { currentItem = context.HotelTypes.FirstOrDefault(p => p.Id == id).MapProperties <HotelTypeVM>(); } return(ServiceResultModel <HotelTypeVM> .OK(currentItem)); }
public ServiceResultModel <HotelDefinitionVM> GetHotel(int id) { HotelDefinitionVM hotel = null; using (EFBookingContext context = new EFBookingContext()) { //context.HotelDefinitions.Include("HotelAttribute"); var hotelEntity = context.HotelDefinitions.FirstOrDefault(p => p.Id == id); hotel = hotelEntity.MapProperties <HotelDefinitionVM>(); hotel.HotelAttributes = hotelEntity.HotelAttributes.Select(p => p.MapProperties <HotelAttributeVM>()).ToList(); hotel.HotelRooms = hotelEntity.HotelRooms.Select(p => p.MapProperties <HotelRoomVM>()).ToList(); } return(ServiceResultModel <HotelDefinitionVM> .OK(hotel)); }
public List <MenuVM> GetMenu() { List <MenuVM> menus = new List <MenuVM>(); using (EFBookingContext context = new EFBookingContext()) { menus = context.Menus.ToList().Select(p => p.MapProperties <MenuVM>()).ToList(); } if (menus == null || !menus.Any()) { return(null); } return(this.CategorizeMenuItem(menus)); }
public ServiceResultModel <RoomTypeVM> GetRoomType(int id) { if (id <= 0) { return(null); } RoomTypeVM currentItem = null; using (EFBookingContext context = new EFBookingContext()) { currentItem = context.RoomTypes.FirstOrDefault(p => p.Id == id).MapToViewModel <RoomTypeVM>(); } return(ServiceResultModel <RoomTypeVM> .OK(currentItem)); }
public ServiceResultModel <List <HotelRoomVM> > GetHotelRooms(HotelRoomFilter filterRequest) { List <HotelRoomVM> hotelRooms = new List <HotelRoomVM>(); using (EFBookingContext context = new EFBookingContext()) { var parameters = new SqlParameter[1]; parameters[0] = new SqlParameter("@HotelId", filterRequest.HotelId); hotelRooms = context.Database .SqlQuery <HotelRoomVM>("GetHotelRooms @HotelId", parameters) .ToList(); } return(ServiceResultModel <List <HotelRoomVM> > .OK(hotelRooms)); }
public ServiceResultModel <UserVM> LoginUser(UserVM user) { User userInfo = null; using (EFBookingContext context = new EFBookingContext()) { userInfo = context.Users.FirstOrDefault(p => p.IsActive && p.EMail == user.EMail && p.Password == user.Password); } if (userInfo == null) { return new ServiceResultModel <UserVM> { Code = ServiceResultCode.NotFound, ResultType = OperationResultType.Warn, Message = "User Not Found, Please check UserName and Password", Data = null } } ; if (!userInfo.IsActive) { return new ServiceResultModel <UserVM> { ResultType = OperationResultType.Warn, Message = "This user is InActive, please contact your administrator", Data = userInfo.MapProperties <UserVM>() } } ; if (!userInfo.EmailConfirmation) { return new ServiceResultModel <UserVM> { Code = ServiceResultCode.EMailIsNotConfirmed, ResultType = OperationResultType.Warn, Message = "Please confirm your account, Check mailbox", Data = userInfo.MapProperties <UserVM>() } } ; return(ServiceResultModel <UserVM> .OK(userInfo.MapProperties <UserVM>())); }
public ServiceResultModel <List <DistrictDefinitionVM> > GetDistrict(int?cityId) { List <DistrictDefinitionVM> resultList = new List <DistrictDefinitionVM>(); using (EFBookingContext context = new EFBookingContext()) { var districts = context.DistrictDefinition.ToList(); if (cityId.HasValue) { districts = districts.Where(p => p.CityId == cityId.Value).ToList(); } resultList.AddRange(districts.Select(p => p.MapToViewModel <DistrictDefinitionVM>())); } return(ServiceResultModel <List <DistrictDefinitionVM> > .OK(resultList)); }
public ServiceResultModel <int> SaveHotelRoom(HotelRoomVM hotel, string mapPath) { using (EFBookingContext context = new EFBookingContext()) { int result = default(int); string savePath = string.Empty; try { HttpPostedFileBase file = hotel.RoomImage; if (file != null && file.ContentLength > 0) { string fileExt = Path.GetExtension(file.FileName); string fileName = string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(file.FileName), DateTime.Now.Ticks, fileExt); savePath = Path.Combine(mapPath, fileName); file.SaveAs(savePath); } context.HotelRooms.Add(new HotelRoom { HotelId = hotel.HotelId, ImageUrl = savePath, MaxCapacity = hotel.RoomCapacity, Price = hotel.Price, RoomTypeId = hotel.RoomTypeId, CreateDate = DateTime.Now, IsActive = true, IsDeleted = false }); result = context.SaveChanges(); } catch (Exception ex) { if (File.Exists(savePath)) { File.Delete(savePath); } // error ekle } return(ServiceResultModel <int> .OK(result)); } }
public ServiceResultModel <bool> SaveHotel(HotelDefinitionVM model) { using (EFBookingContext context = new EFBookingContext()) { using (var transaction = context.Database.BeginTransaction()) { try { var deletedAttributes = context.HotelAttributes.Where(p => p.HotelId == model.Id); context.HotelAttributes.RemoveRange(deletedAttributes); HotelDefinition hotel = new HotelDefinition(); hotel = model.MapProperties <HotelDefinition>(); context.HotelDefinitions.Add(hotel); var savedHotel = context.SaveChanges(); foreach (var item in model.Attributes.Where(p => p.IsSelected)) { context.HotelAttributes.Add(new HotelAttribute { AttributeId = item.Id, HotelId = hotel.Id, IsActive = true, IsDeleted = false, CreateDate = DateTime.Now }); // throw new Exception(); } context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw; } } } return(ServiceResultModel <bool> .OK(true)); }
public ServiceResultModel <List <CityDefinitionVM> > GetCities() { List <CityDefinitionVM> resultList = new List <CityDefinitionVM>(); using (EFBookingContext context = new EFBookingContext()) { var cityDefs = context.CityDefinitions.Select(p => p).ToList(); cityDefs.ForEach(p => { CityDefinitionVM cityVM = new CityDefinitionVM(); cityVM = p.MapToViewModel <CityDefinitionVM>(); cityVM.Districts = p.Districts.Select(c => c.MapToViewModel <DistrictDefinitionVM>()).ToList(); resultList.Add(cityVM); }); } return(ServiceResultModel <List <CityDefinitionVM> > .OK(resultList)); }
public ServiceResultModel <List <RoomTypeVM> > GetAllRoomTypes(RoomTypeFilter filter) { List <RoomTypeVM> resultList = new List <RoomTypeVM>(); using (EFBookingContext context = new EFBookingContext()) { IQueryable <RoomType> roomTypeList = context.RoomTypes; if (filter.Title.IsNotNull()) { roomTypeList = roomTypeList.Where(p => p.Title.Contains(filter.Title)); } roomTypeList.ToList().ForEach(p => { resultList.Add(p.MapToViewModel <RoomTypeVM>()); }); return(ServiceResultModel <List <RoomTypeVM> > .OK(resultList)); } }
public ServiceResultModel <List <ProcedureVM> > GetAllStokDefinition(STKFilter filter) { List <ProcedureVM> resultList = new List <ProcedureVM>(); using (var context = new EFBookingContext()) { if (filter.MalKodu == null || filter.MalAdı == null) { filter.MalKodu = "10087 SİEMENS"; filter.MalAdı = "5SQ2160-2YA40 B 40A OTOMATİK SİGORTA"; } var MalKodu = new SqlParameter("@MalKodu", filter.MalKodu); var result = context.Database.SqlQuery <ProcedureVM>("Mal_Procedure_ @MalKodu", MalKodu).ToList(); resultList = result.ToList(); return(ServiceResultModel <List <ProcedureVM> > .OK(resultList)); } }
public ServiceResultModel <AttributeVM> SaveAttribute(AttributeVM model) { using (EFBookingContext context = new EFBookingContext()) { if (context.Attributes.Any(p => p.Name.Equals(model.Name) && p.AttributeType == model.AttributeType)) { return(new ServiceResultModel <AttributeVM> { Code = ServiceResultCode.Duplicate, Data = model, ResultType = OperationResultType.Warn, Message = "This record already exitst " }); } var recordItem = context.Attributes.Add(model.MapToEntityModel <Attributes>()); context.SaveChanges(); return(ServiceResultModel <AttributeVM> .OK(recordItem.MapToViewModel <AttributeVM>())); } }
public ServiceResultModel <RoomTypeVM> SaveRoomType(RoomTypeVM model) { using (EFBookingContext context = new EFBookingContext()) { bool isAlreadyExists = context.RoomTypes.Any(p => p.Title == model.Title); if (isAlreadyExists) { return(new ServiceResultModel <RoomTypeVM> { Code = ServiceResultCode.Duplicate, Data = null, ResultType = OperationResultType.Warn, Message = "This record already exists" }); } var recordItem = context.RoomTypes.Add(model.MapToEntityModel <RoomType>()); context.SaveChanges(); return(ServiceResultModel <RoomTypeVM> .OK(recordItem.MapToViewModel <RoomTypeVM>())); } }
public ServiceResultModel <HotelTypeVM> DeleteHotelType(int id) { using (EFBookingContext context = new EFBookingContext()) { var deleteItem = context.HotelTypes.FirstOrDefault(p => p.Id == id); context.HotelTypes.Remove(deleteItem); context.SaveChanges(); return(ServiceResultModel <HotelTypeVM> .OK(deleteItem.MapToViewModel <HotelTypeVM>())); /* * veya bu şeklide de yazabilirsiniz. * EF Tracking sistemi ile çalışır. 2. kodda tracking 'e deleteıtem kaydının Hoteltype tablosunda deleted olarak Track'lendiğini bildiriyoruz. * sonrasında commit 'de ilgili kayıt silinir. * * var deleteItem = context.HotelTypes.FirstOrDefault(p => p.Id == id); * context.Entry<HotelType>(deleteItem).State = System.Data.Entity.EntityState.Deleted; * context.SaveChanges(); * */ } }
public ServiceResultModel <List <AttributeVM> > GetAllAttributeList(AttributeFilter filter) { List <AttributeVM> resultList = new List <AttributeVM>(); using (EFBookingContext context = new EFBookingContext()) { IEnumerable <Attributes> attributeList = context.Attributes; if (filter.Name.IsNotNull()) { attributeList = attributeList.Where(p => p.Name.Equals(filter.Name)); } // Foreach çalışırken listedeki elemanları sırasıyla her sefer 1 eleman şeklinde döner. // Paralel üzerinde foreach çalıştırılırsa sıra bağımsız option varsa option init'e göre // işlemi çok daha kısa sürede tamamlar. // Paralelde dikkat edilmesi gereken en önemli konu aynı kayıda birden fazla thread'ın erişip // aynı kaydı set edebilme durumudur. Microsoft buna garanti vermiyor. // burada Item Lock ile kitlenebilir veya concurency ( thread-safe ) list kullanılablir // https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.paralleloptions.maxdegreeofparallelism?redirectedfrom=MSDN&view=netframework-4.8#System_Threading_Tasks_ParallelOptions_MaxDegreeOfParallelism // link'i inceleyiniz. var syncLockObj = new object(); Parallel.ForEach(attributeList, new ParallelOptions { MaxDegreeOfParallelism = 4 }, currentAttribute => { lock (syncLockObj) { resultList.Add(currentAttribute.MapToViewModel <AttributeVM>()); } }); } return(ServiceResultModel <List <AttributeVM> > .OK(resultList)); }
public ServiceResultModel <HotelVM> SaveHotel(HotelVM model) { using (EFBookingContext context = new EFBookingContext()) { bool isAlreadyExists = context.Hotels.Any(p => p.Title == model.Title); if (isAlreadyExists) { return new ServiceResultModel <HotelVM> { Code = ServiceResultCode.Duplicate, Data = null, ResultType = OperationResultType.Warn, Message = "This record already exists" } } ; var recordItem = context.Hotels.Add(model.MapProperties <Hotel>()); context.SaveChanges(); return(ServiceResultModel <HotelVM> .OK(recordItem.MapProperties <HotelVM>())); } }
public ServiceResultModel <List <HotelVM> > GetAllHotels(HotelFilter filter) { List <HotelVM> resultList = new List <HotelVM>(); using (EFBookingContext context = new EFBookingContext()) { IQueryable <Hotel> HotelList = context.Hotels; if (filter.Name.IsNotNull()) { HotelList = HotelList.Where(p => p.Name.Contains(filter.Name)); } //if (filter.HotelTypeId.HasValue) // HotelList = HotelList.Where(p => p.HotelTypeId == filter.HotelTypeId); HotelList.ToList().ForEach(p => { resultList.Add(p.MapProperties <HotelVM>()); }); return(ServiceResultModel <List <HotelVM> > .OK(resultList)); } }