Skip to content

Commit

Permalink
event participants
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivar Abrahamsen committed May 12, 2012
1 parent 128671d commit 1943ea4
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 32 deletions.
10 changes: 5 additions & 5 deletions app/controllers/AlbumController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object AlbumController extends Controller with Secured {

val albumForm = Form(
tuple(
"publisher" -> nonEmptyText(minLength = 3, maxLength = 100),
"publisher" -> optional(text(minLength = 3, maxLength = 100)),
"url" -> nonEmptyText(minLength = 8, maxLength = 150)
)
)
Expand All @@ -25,12 +25,12 @@ object AlbumController extends Controller with Secured {
Logger.warn("Event not found on show album")
NotFound
}
case Some(event) => Ok(views.html.albums.add(event,albumForm.fill(participant.username,"")))
case Some(event) => Ok(views.html.albums.add(event,albumForm.fill(Some(participant.username),"")))
}
}


def addAlbum(eventId: Long) = isAuthenticated { username => implicit request =>
def addAlbum(eventId: Long) = withParticipant { participant => implicit request =>
Event.findEvent(eventId) match {
case None => {
Logger.warn("Event not found on add album ")
Expand All @@ -45,7 +45,7 @@ object AlbumController extends Controller with Secured {
BadRequest(views.html.albums.add(event,errors))
},
submittedAlbumForm => {
val album = new Album(submittedAlbumForm._1,submittedAlbumForm._2)
val album = new Album(participant.username,submittedAlbumForm._2)
event.addAlbum(album)
Redirect(routes.EventController.viewEvent(eventId));
}
Expand Down Expand Up @@ -77,7 +77,7 @@ object AlbumController extends Controller with Secured {
},
submittedAlbumForm => {
val updatedAlbum = album.copy(
publisher = submittedAlbumForm._1,
publisher = submittedAlbumForm._1.getOrElse(username),
url = submittedAlbumForm._2)
Album.updateAlbum(updatedAlbum)
Redirect(routes.EventController.showEditEvent(eventId));
Expand Down
16 changes: 9 additions & 7 deletions app/controllers/EventController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object EventController extends Controller with Secured {
NotFound
}
case Some(event) => {
if(event.public){
if(event.public || !event.organiser.isDefined){
Ok(views.html.events.view(event))
} else {
currentParticipant match {
Expand All @@ -68,8 +68,10 @@ object EventController extends Controller with Secured {
if(event.isParticipant(participant) ) {
Ok(views.html.events.view(event))
} else {
Logger.warn("Current participant is not a participant: " + participant )
Forbidden
Logger.warn("Current participant is not a participant: " + participant.username )
Logger.warn("Event organiser: " + event.organiser.get.username )
// Unauthorized(views.html.login(Application.loginForm)).flashing("message"->"Event private, and you do not have access to it")
Unauthorized(views.html.events.unauthorised(event)).flashing("message"->"Event private, and you do not have access to it")
}
}
}
Expand Down Expand Up @@ -113,7 +115,7 @@ object EventController extends Controller with Secured {
Ok(views.html.events.edit(event,albums,editForm))
} else {
Logger.warn("Not an organiser:" + currentParticipant.get + " | " + event.organiserId)
Forbidden
Unauthorized
}
}
}
Expand Down Expand Up @@ -146,7 +148,7 @@ object EventController extends Controller with Secured {
Redirect(routes.EventController.viewEvent(event.eventId));
} else {
Logger.warn("Not an organiser")
Forbidden
Unauthorized
}
}
)
Expand All @@ -166,7 +168,7 @@ object EventController extends Controller with Secured {
Ok(views.html.events.delete(event))
} else {
Logger.warn("Not an organiser")
Forbidden
Unauthorized
}
}
}
Expand All @@ -186,7 +188,7 @@ object EventController extends Controller with Secured {
Redirect(routes.Application.index()).flashing("message" -> "Event deleted");
} else {
Logger.warn("Not an organiser")
Forbidden
Unauthorized
}
}
}
Expand Down
29 changes: 21 additions & 8 deletions app/models/Event.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ case class Event (
public: Boolean = true
){
require(eventName.trim.length > 1)
require( organiserId.isDefined || public )
// require(description == None || description.get.trim.length < 3900)


Expand All @@ -29,15 +30,10 @@ case class Event (

def this(eventId: Long, that: Event) = this(eventId,that.eventName, that.organiserId, that.eventDate, that.description)

val organiser = organiserId.map { participantId =>
val organiser : Option[Participant] = organiserId.map { participantId =>
Participant.findById(participantId)
}.getOrElse(None)

// def this(eventId: Long, eventName: String, organiser: Option[String], eventDate: Option[String], description: Option[String], albums: List[Album]) {
// this(eventId, eventName, organiser, eventDate, description)
// this.albums = albums;
// }

def findAlbum(albumId: Long) = {
Album.findAlbum(eventId,albumId)
}
Expand All @@ -51,16 +47,17 @@ case class Event (
}

def isParticipant(participant: Participant) = {
isOrganiser(participant)
Event.isParticipant(eventId,participant.participantId) || isOrganiser(participant)
}

def isOrganiser(participant: Participant) = {
organiser == participant
organiser.map { organiser => organiser == participant }.getOrElse(false)
}

}



object Event {


Expand Down Expand Up @@ -209,4 +206,20 @@ object Event {
}
}

def isParticipant(eventId: Long, participantId: Long) : Boolean = {
DB.withConnection { implicit connection =>
SQL(
"""
select count(participantid) = 1
from eventparticipant
where eventid = {eventid}
and participantid = {participantid}
"""
).on(
'participantid -> participantId,
'eventid -> eventId
).as(scalar[Boolean].single)
}
}

}
2 changes: 1 addition & 1 deletion app/views/albums/add.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(event: Event, albumForm: Form[(String,String)])(implicit currentParticipant: Option[Participant])
@(event: Event, albumForm: Form[(Option[String],String)])(implicit currentParticipant: Option[Participant])

@import helper._
@import helper.twitterBootstrap._
Expand Down
30 changes: 30 additions & 0 deletions app/views/events/unauthorised.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@(event: Event)(implicit currentParticipant: Option[Participant])

@import helper._
@import helper.twitterBootstrap._
@import models._

@main(" snaps | Private event "){
<li>
<span class="divider">/</span>
<a href="@routes.EventController.viewEvent(event.eventId)">Event</a>
</li>
} {


<h3>Event: @event.eventName: </h3>

<br/>

<div class="alert alert-block">
This event is private and
unfortunately you do not have access to it.
</div>

<div class="alert alert-block">
You may contact the organiser @event.organiser.map{ organiser => , <em>@organiser.username</em>, }.getOrElse("") and ask to be a participant.
</div>



}
20 changes: 14 additions & 6 deletions app/views/events/view.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ <h3>Photo albums</h3>

<div class="form-actions">

@form(action = routes.AlbumController.showAddAlbum(event.eventId), 'class -> "form-inline"){
<button type="submit" class="btn btn-info btn-small">Add album</button>
}
@if(currentParticipant.isDefined){
@form(action = routes.EventController.showEditEvent(event.eventId), 'class -> "form-inline"){
<button type="submit" class="btn btn-warning btn-small">Edit event</button>
@if( currentParticipant.isDefined ){
@if( event.isParticipant(currentParticipant.get) ){
@form(action = routes.AlbumController.showAddAlbum(event.eventId), 'class -> "form-inline"){
<button type="submit" class="btn btn-info btn-small">Add album</button>
}
}
@if( event.isOrganiser(currentParticipant.get) ){
@form(action = routes.EventController.showEditEvent(event.eventId), 'class -> "form-inline"){
<button type="submit" class="btn btn-warning btn-small">Edit event</button>
}
}
@if( !event.isParticipant(currentParticipant.get) ){
<button type="button" disabled="disabled" class="btn btn-info btn-small">Join event</button>
}
}

</div>


Expand Down
13 changes: 12 additions & 1 deletion conf/evolutions/default/1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

SET REFERENTIAL_INTEGRITY FALSE;

DROP TABLE IF EXISTS eventparticipant;

DROP TABLE IF EXISTS snapalbum;

DROP TABLE IF EXISTS snapevent;
Expand Down Expand Up @@ -36,14 +38,21 @@ CREATE TABLE snapalbum (
foreign key(eventid) references snapevent(eventid) on delete cascade
);

CREATE TABLE PARTICIPANT (
CREATE TABLE participant (
participantid SERIAL PRIMARY KEY,
username VARCHAR(100) UNIQUE,
fullname VARCHAR(100),
email VARCHAR(100),
password VARCHAR(100) NOT NULL
);

CREATE TABLE eventparticipant (
eventid BIGINT NOT NULL,
participantid BIGINT NOT NULL,
foreign key(eventid) references snapevent(eventid) on delete cascade,
foreign key(participantid) references participant(participantid) on delete cascade
);

CREATE SEQUENCE snapevent_seq START WITH 1000;

CREATE SEQUENCE snapalbum_seq START WITH 1000;
Expand All @@ -58,6 +67,8 @@ CREATE SEQUENCE participant_seq START WITH 1000;

SET REFERENTIAL_INTEGRITY FALSE;

DROP TABLE IF EXISTS eventparticipant;

DROP TABLE IF EXISTS participant;

DROP TABLE IF EXISTS snapalbum;
Expand Down
21 changes: 18 additions & 3 deletions conf/evolutions/default/3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ INSERT INTO snapevent (eventid,eventname,organiserid,eventdate) VALUES (

INSERT INTO snapevent (eventid,eventname,eventdate,publicevent) VALUES (
(SELECT NEXTVAL('snapevent_seq')),
'Dieter''s birthday party', '2010-12-13', FALSE );
'Dieter''s birthday party', '2010-12-13', true );

INSERT INTO snapevent (eventid,eventname, eventdate) VALUES (
(SELECT NEXTVAL('snapevent_seq')),
'Henriette''s birthday party', '2010-12-13' );

INSERT INTO snapevent (eventid,eventname,organiserid,eventdate) VALUES (
INSERT INTO snapevent (eventid,eventname,organiserid,eventdate,publicevent) VALUES (
(SELECT NEXTVAL('snapevent_seq')),
'Lucy Ann''s birthday party',
(SELECT MAX(participantid) FROM participant WHERE username = 'anotheruser'),
'2010-12-13' );
'2010-12-13' , FALSE);

INSERT INTO snapevent (eventid,eventname,eventdate,publicevent) VALUES (
(SELECT NEXTVAL('snapevent_seq')),
Expand Down Expand Up @@ -66,6 +66,8 @@ INSERT INTO snapevent (eventid,eventname,eventdate,description) VALUES (
(SELECT NEXTVAL('snapevent_seq')),
'Christmas', 'Christmas 2011','Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.' );



INSERT INTO snapalbum (albumid,publisher,url,eventid) VALUES (
(SELECT NEXTVAL('snapalbum_seq')),
'John Smith', 'http://flickr.com/photos/flurdy/set/12121eqweewqwqe',
Expand All @@ -78,6 +80,19 @@ INSERT INTO snapalbum (albumid,publisher,url,eventid) VALUES (



INSERT INTO eventparticipant (eventid,participantid) VALUES (
(SELECT MAX(eventid) FROM snapevent WHERE eventname = 'Christmas at Smiths'),
(SELECT MAX(participantid) FROM participant WHERE username = 'anotheruser')
);

INSERT INTO eventparticipant (eventid,participantid) VALUES (
(SELECT MAX(eventid) FROM snapevent WHERE eventname = 'Christmas at Smiths'),
(SELECT MAX(participantid) FROM participant WHERE username = 'testuser')
);




# --- !Downs


Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ logLevel := Level.Warn
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.0")
addSbtPlugin("play" % "sbt-plugin" % "2.0.1")

0 comments on commit 1943ea4

Please sign in to comment.