c# - Room availability between two dates (beginner) -


i'm using visual studio community 2013 , working on project class in making very simple hotel booking system used staff create new customers , book them room.

when user tries book same room twice or overlaps need system not allow , i'm trying take care of in service. (see below)

public override void add(booking booking) {     // don't allow new booking if room out.      var currentbooking = _ctx.bookings         .where(b => b.roomid == booking.roomid)         .select(b => (b.checkout < booking.checkin                         && b.checkin  < booking.checkin)                        || (b.checkin > booking.checkout                         && b.checkout > booking.checkout ))         .firstordefault();      if (currentbooking != null)     {         throw new bookingexception("the room out on date.");     }      booking.checkin = datetime.now.date;       _ctx.set<booking>().add(booking);     _ctx.savechanges(); } 

the problem i'm having no matter date put in when creating new booking system throws bookingexception "room out", though dbseed has 2 dates in it. (see below)

context.bookings.addorupdate(             b => b.checkin,             new booking()             {                 checkin = new datetime(2015, 09, 12),                                     checkout = new datetime(2015, 09, 21),                 roomid = 1,                 customerid = 1,             },             new booking()             {                 checkin = new datetime(2015, 06, 01),                 checkout = new datetime(2015, 06,08),                 roomid = 2,                 customerid = 2             }); 

i'm thinking problem in service, having trouble figuring out specific problem is.

this currentbooking bool hence throwing exception, ie it's never null.

switching if statement check true seemed highlight issue filtering in linq. have rewrote select this, i've run couple of tests seems work:

        currentbooking = _ctx.booking             .where(b => b.roomid == booking.roomid)             .select(b => (booking.checkin < b.checkout && booking.checkin > b.checkin))             .firstordefault();          if (currentbooking)         {             throw new exception("the room out on date.");         } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -