OSDN Git Service

150f8097b75534dc6490c728731fff46124fcb27
[sharp4k/CUTEn.git] / CutenServer / app / controllers / tasks_controller.rb
1 class TasksController < ApplicationController
2   # GET /tasks/1
3   # GET /tasks/1.json
4   def show
5     @task = Task.find(params[:id])
6
7     render json: @task
8   end
9
10   # GET /lectures/:lecture_id/tasks/new
11   def new
12     authenticate_user!
13     teacher = current_user.teacher
14
15     @task = Task.new
16     @task.lecture = Lecture.find params[:lecture_id]
17     @apk = Apk.new
18
19     respond_to do |format|
20       if teacher and @task.lecture.teacher.id == teacher.id
21         format.html # new.html.haml
22       else
23         format.html { redirect_to @task.lecture, :alert => I18n.t('task.create.unauthorized') }
24       end
25     end
26   end
27
28   # POST /lectures/:lecture_id/tasks
29   def create
30     authenticate_user!
31     teacher = current_user.teacher
32     @task = Task.new params[:task]
33     @task.lecture = Lecture.find params[:lecture_id]
34     @apk = Apk.new params[:apk]
35     uploaded_file = params[:apkfile] && params[:apkfile][:file]
36
37     respond_to do |format|
38       format.html do
39         if teacher and @task.lecture.teacher.id == teacher.id
40           if uploaded_file
41             # TODO: check if the uploaded apk is correct
42             if @apk.save
43               @apk.store_file uploaded_file.tempfile
44
45               @task.apks << @apk
46               if @task.save
47                 redirect_to @task.lecture, :notice => I18n.t('task.create.success')
48               else
49                 render :action => 'new'
50               end
51             else
52               render :action => 'new'
53             end
54           else
55             @noapk = I18n.t('task.create.noapk')
56             render :action => 'new'
57           end
58         else
59           redirect_to @task.lecture, :alert => I18n.t('task.create.unauthorized')
60         end
61       end
62     end
63   end
64 end