[go: nahoru, domu]

Skip to content

Commit

Permalink
added edit building form
Browse files Browse the repository at this point in the history
  • Loading branch information
dynamic11 committed Jul 6, 2019
1 parent 18e778e commit 17112bb
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 76 deletions.
53 changes: 53 additions & 0 deletions app/Controllers/Http/BuildingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,59 @@ class BuildingController {

return response.route('home');
}

/**
* store the selected building in the cookie
*
* @param {Object} Context The context object.
*/
async editBuilding ({ response, params, view }) {
try {

const building = await Building.query()
.where('id', params.id)
.firstOrFail();

let actionType="Edit Building"

return view.render('adminPages.addEditBuilding', { building: building.toJSON(), actionType });
} catch (err) {
console.log(err);
}

return response.route('home');
}

/**
* store the selected building in the cookie
*
* @param {Object} Context The context object.
*/
async updateBuilding ({ response, params, request }) {
try {

// Retrieves user input
const body = request.all();

// Updates room information in database
await Building
.query()
.where('id', params.id)
.update({
name: body.name,
street_address: body.streetAddress,
postal_code: body.postalCode,
city: body.city,
country: "Canada"
})

return response.route('configuration');
} catch (err) {
console.log(err);
}

return response.route('home');
}
}

module.exports = BuildingController;
106 changes: 53 additions & 53 deletions app/Controllers/Http/RoomController.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,65 +944,65 @@ class RoomController {
*
* @param {Object} Context The context object.
*/
async getBookings ({ params, view, auth, response }) {
var canEdit = 0;
var layoutType = '';
const userRole = await auth.user.getUserRole();

if (userRole === 'admin') {
layoutType = 'layouts/adminLayout';
canEdit = 1;
// check if user is viewing their own profile
} else if (auth.user.id === Number(params.id) && userRole === 'user') {
layoutType = 'layouts/mainLayout';
canEdit = 1;

// check if user is viewing someone elses profile
} else if (auth.user.id !== Number(params.id) && userRole === 'user') {
layoutType = 'layouts/mainLayout';
canEdit = 0;
} else {
return response.redirect('/');
}

// Queries the database fr the bookings associated to a specific room
let searchResults = await Booking
.query()
.where('room_id', params.id)
.fetch();

searchResults = searchResults.toJSON();
const bookings = await populateBookings(searchResults);

return view.render('userPages.manageBookings', { bookings, layoutType, canEdit });
}
// async getBookings ({ params, view, auth, response }) {
// var canEdit = 0;
// var layoutType = '';
// const userRole = await auth.user.getUserRole();

// if (userRole === 'admin') {
// layoutType = 'layouts/adminLayout';
// canEdit = 1;
// // check if user is viewing their own profile
// } else if (auth.user.id === Number(params.id) && userRole === 'user') {
// layoutType = 'layouts/mainLayout';
// canEdit = 1;

// // check if user is viewing someone elses profile
// } else if (auth.user.id !== Number(params.id) && userRole === 'user') {
// layoutType = 'layouts/mainLayout';
// canEdit = 0;
// } else {
// return response.redirect('/');
// }

// // Queries the database fr the bookings associated to a specific room
// let searchResults = await Booking
// .query()
// .where('room_id', params.id)
// .fetch();

// searchResults = searchResults.toJSON();
// const bookings = await populateBookings(searchResults);

// return view.render('userPages.manageBookings', { bookings, layoutType, canEdit });
// }

/**
* Create a list of all bookings under the current user and render a view for it.
*
* @param {Object} Context The context object.
*/
async viewUserBookings ({ params, auth, view, response }) {
var canEdit = 0;
var layoutType = '';
const userRole = await auth.user.getUserRole();

if (userRole === 'admin') {
layoutType = 'layouts/adminLayout';
canEdit = 1;
// check if user is viewing their own profile
} else if (auth.user.id === Number(params.id) && userRole === 'user') {
layoutType = 'layouts/mainLayout';
canEdit = 1;
} else {
return response.redirect('/');
}

const results = (await auth.user.bookings().fetch()).toJSON();
const bookings = await populateBookings(results);

return view.render('userPages.manageUserBookings', { bookings, layoutType, canEdit });
}
// async viewUserBookings ({ params, auth, view, response }) {
// var canEdit = 0;
// var layoutType = '';
// const userRole = await auth.user.getUserRole();

// if (userRole === 'admin') {
// layoutType = 'layouts/adminLayout';
// canEdit = 1;
// // check if user is viewing their own profile
// } else if (auth.user.id === Number(params.id) && userRole === 'user') {
// layoutType = 'layouts/mainLayout';
// canEdit = 1;
// } else {
// return response.redirect('/');
// }

// const results = (await auth.user.bookings().fetch()).toJSON();
// const bookings = await populateBookings(results);

// return view.render('userPages.manageUserBookings', { bookings, layoutType, canEdit });
// }

/**
* Calcualtes the average rating of a specific room, based off of the room Id
Expand Down
31 changes: 31 additions & 0 deletions app/Validators/EditBuilding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* File Name: AddRoom.js
* Description: Validator used to validate input fields in Add Room Page (Admin)
* Instructions: Use this validator by adding ".validator('AddRoom')" to your route in /routes.js
**/
'use strict';

class EditBuilding {
// Validate and return all fields
get validateAll () {
return true;
}

// Validation rules
get rules () {
return {
name: 'required|max:50',
streetAddress: 'required|max:50',
postalCode: 'required|max:50',
city: 'required|max:50',
};
}

get messages () {
return {
'required': 'This field is required.',
}
}
}

module.exports = EditBuilding;
111 changes: 111 additions & 0 deletions resources/views/adminPages/addEditBuilding.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
@layout('layouts.mainLayout')

@section('title')
{{ actionType }}
@endsection

@section('content')
@if(actionType =='Add Building')
@!component('components.pageHeader', title='Add building')
<p>Add building to database. Rooms will be assigned to buildings.</p>
@else
@!component('components.pageHeader', title='Edit building')
@endif

<div class='row'>
<div class= 'col-12 col-lg-9'>
<div class="card shadow mb-4 p-3">
<div class="card-body">
<div class='row'>
<div class='col-md-12'>
@if(actionType =='Add Building')
<form action='{{ route('addBuilding') }}' method='POST' enctype='multipart/form-data'>
@else
<form action='{{ route('editBuilding', {id: building.id}) }}' method='POST' enctype='multipart/form-data'>
@endif
{{ csrfField() }}
<div class='row'>
<div class='col form-group'>
<div class='row'>
<label class='col' for='name'>Building name <strong class='text-danger'>*</strong></label>
</div>
@if(hasErrorFor('name'))
<input type='text' name='name' class='form-control is-invalid' id='name' placeholder='Ex: 101A (E)' value='{{ old('name', building.name) }}' oninput='removeErrorClass("name")'>
<div class='invalid-feedback'>{{ getErrorFor('name') }}</div>
@else
<input type="text" name="name" class="form-control" id="name" placeholder="Ex: 101A (E)" value="{{ old('name', building.name) }}" >
@endif
</div>
</div>
{{-- Full Room Name form --}}
<div class='row'>
<div class='col form-group'>
<div class='row'>
<label class='col' for='fullName'>Street Address<strong class='text-danger'>*</strong></label>
</div>
@if(hasErrorFor('streetAddress'))
<input type='text' name='streetAddress' class='form-control is-invalid' id='streetAddress' placeholder='235 Queen Street' value='{{ old('streetAddress', building.street_address) }}' oninput='removeErrorClass("streetAddress")'/>
<div class='invalid-feedback'>{{ getErrorFor('streetAddress') }}</div>
@else
<input type='text' name='streetAddress' class='form-control' id='streetAddress' placeholder='235 Queen Street' value='{{ old('streetAddress', building.street_address) }}'/>
@endif
</div>
</div>

{{-- Full Room Name form --}}
<div class='row'>
<div class='col form-group'>
<div class='row'>
<label class='col' for='fullName'>City<strong class='text-danger'>*</strong></label>
</div>
@if(hasErrorFor('city'))
<input type='text' name='city' class='form-control is-invalid' id='city' placeholder='Ottawa' value='{{ old('city', building.city) }}' oninput='removeErrorClass("city")'/>
<div class='invalid-feedback'>{{ getErrorFor('city') }}</div>
@else
<input type='text' name='city' class='form-control' id='city' placeholder='Ottawa' value='{{ old('city', building.city) }}'/>
@endif
</div>
</div>

{{-- Full Room Name form --}}
<div class='row'>
<div class='col form-group'>
<div class='row'>
<label class='col' for='fullName'>Postal Code<strong class='text-danger'>*</strong></label>
</div>
@if(hasErrorFor('postalCode'))
<input type='text' name='postalCode' class='form-control is-invalid' id='postalCode' placeholder='A2A4Y4' value='{{ old('postalCode', building.postal_code) }}' oninput='removeErrorClass("postalCode")'/>
<div class='invalid-feedback'>{{ getErrorFor('postalCode') }}</div>
@else
<input type='text' name='postalCode' class='form-control' id='postalCode' placeholder='A2A4Y4' value='{{ old('postalCode', building.postal_code) }}'/>
@endif
</div>
</div>

<div class='row' style='margin-top:20px;'>
<div class='col text-right'>
<button type='button' class='btn btn-secondary mb-3' onclick="window.location='{{ route('adminDash')}}';">Cancel</button>
@if(actionType =='Add Room')
<button type='submit' class='btn btn-primary mb-3 mr-0'>Add Building</button>
@else
<button type='submit' class='btn btn-primary mb-3 mr-0'>Update</button>
@endif
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>

@endsection








46 changes: 27 additions & 19 deletions resources/views/adminPages/addEditRoom.edge
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
@endsection

@section('content')
@!component('components.pageHeader', title='Add room')
@if(actionType =='Add Room')
@!component('components.pageHeader', title='Add room')
<p>Add rooms to database. Rooms added here can be booked by users.</p>
@else
@!component('components.pageHeader', title='Edit room')
@endif
@if(flashMessage('error'))
<div class="row">
<div class="col-xl-12">
Expand All @@ -16,7 +21,7 @@
</div>
</div>
@endif
<p>Add rooms to database. Rooms added here can be booked by users.</p>


<div class='row'>
<div class= 'col-12 col-lg-9'>
Expand Down Expand Up @@ -275,23 +280,26 @@
{{-- displaying the room picture --}}
@!component('components.imageModal', id="room-roompicture-"+room.id, title=room.name + ' - Room picture', image='/'+ room.picture)

{{-- Script to display file name on upload --}}
<script>
$('#floorPlan').change(function(e){
var fileName = e.target.files[0].name;
$('#floorPlanLabel').html(fileName);
});
$('#roomPicture').change(function(e){
var fileName = e.target.files[0].name;
$('#roomPictureLabel').html(fileName);
});
</script>
{{-- Remove the is-invalid class when input changes --}}
<script>
function removeErrorClass(idOfInput){
$('#'+idOfInput).removeClass('is-invalid');
}
</script>
@section('extrajs')
{{-- Script to display file name on upload --}}
<script>
$('#floorPlan').change(function(e){
var fileName = e.target.files[0].name;
$('#floorPlanLabel').html(fileName);
});
$('#roomPicture').change(function(e){
var fileName = e.target.files[0].name;
$('#roomPictureLabel').html(fileName);
});
</script>
{{-- Remove the is-invalid class when input changes --}}
<script>
function removeErrorClass(idOfInput){
$('#'+idOfInput).removeClass('is-invalid');
}
</script>
@endsection

@endsection


Expand Down
Loading

0 comments on commit 17112bb

Please sign in to comment.