<commit_before>//
//  PodcastController.swift
//  DebuggedPodcast
//
//  Created by <NAME> on 4/8/17.
//
//

import Vapor
import HTTP
import Foundation
import FormData

class PodcastController: ResourceRepresentable {
    func index(request: Request) throws -> ResponseRepresentable {
        return try Podcast.all().makeNode().converted(to: JSON.self)
    }
    
    func create(request: Request) throws -> ResponseRepresentable {
        let user = try requireAuth(req: request)
        let metadata = try Metadata.all().first
        
        let params = try Validator.validate(req: request, expected:
            [
                K.API.Title : Type.String,
                K.API.Subtitle : Type.String,
                K.API.Author : Type.String,
                K.API.Summary : Type.String,
                K.API.ImageURL : Type.String,
                K.API.MediaDuration : Type.String
            ])
        
        guard let file = request.data[K.API.Media] as? FormData.Field else {
            throw Abort.custom(status: Status.badRequest, message: "media should be type: Multipart Binary")
        }
        
        guard let filename = file.filename else {
            throw Abort.badRequest
        }
        
        try Data(file.part.body).write(to: URL(fileURLWithPath: drop.workDir + "/Public/media/\(filename)"))
        
        let df:DateFormatter = DateFormatter()
        df.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format
        df.locale = Locale(identifier: "en_US_POSIX")
        let date:String = df.string(from: Date())
        
        var podcast = Podcast(title: params.string[K.API.Title] ?? "",
                              author: params.string[K.API.Author] ?? "",
                              subtitle: params.string[K.API.Subtitle] ?? "",
                              summary: params.string[K.API.Summary] ?? "",
                              imageURL: params.string[K.API.ImageURL] ?? "",
                              mediaURL: "./media/\(filename)",
                              mediaLength: file.part.body.count,
                              mediaType: "audio/mpeg",
                              mediaDuration: params.string[K.API.MediaDuration] ?? "",
                              GUID: UUID().uuidString,
                              publishDate: date,
                              createdBy: user.id?.int ?? -1,
                              metadata: metadata?.id?.int ?? -1)
        
        try podcast.save()
        return podcast
    }
    
    func show(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        return podcast
    }
    
    func delete(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        _ = try requireAuth(req: request)
        
        try podcast.delete()
        return JSON([:])
    }
    
    func clear(request: Request) throws -> ResponseRepresentable {
        _ = try requireAuth(req: request)
        
        try Podcast.query().delete()
        return JSON([])
    }
    
    func update(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        let user = try requireAuth(req: request)
        let metadata = try Metadata.all().first
        
        let params = try Validator.validate(req: request, expected:
            [
                K.API.Title : Type.String,
                K.API.Subtitle : Type.String,
                K.API.Author : Type.String,
                K.API.Summary : Type.String,
                K.API.ImageURL : Type.String,
                K.API.MediaURL : Type.String,
                K.API.MediaLength : Type.Int,
                K.API.MediaType : Type.String,
                K.API.MediaDuration : Type.String,
                K.API.GUID : Type.String,
                K.API.PublishDate : Type.String,
                ])
        
        
        var podcast = Podcast(title: params.string[K.API.Title] ?? "",
                              author: params.string[K.API.Author] ?? "",
                              subtitle: params.string[K.API.Subtitle] ?? "",
                              summary: params.string[K.API.Summary] ?? "",
                              imageURL: params.string[K.API.ImageURL] ?? "",
                              mediaURL: params.string[K.API.MediaURL] ?? "",
                              mediaLength: params.int[K.API.MediaLength] ?? -1,
                              mediaType: params.string[K.API.MediaType] ?? "",
                              mediaDuration: params.string[K.API.MediaDuration] ?? "",
                              GUID: params.string[K.API.GUID] ?? "",
                              publishDate: params.string[K.API.PublishDate] ?? "",
                              createdBy: user.id?.int ?? -1,
                              metadata: metadata?.id?.int ?? -1)
        
        try podcast.save()
        return podcast
    }
    
    func replace(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        _ = try requireAuth(req: request)
        try podcast.delete()
        return try create(request: request)
    }
    
    func makeResource() -> Resource<Podcast> {
        return Resource(
            index: index,
            store: create,
            show: show,
            replace: replace,
            modify: update,
            destroy: delete,
            clear: clear
        )
    }
    
    private func requireAuth(req: Request) throws -> User {
        guard let user = try req.auth.user() as? User else {
            throw Abort.custom(status: Status.unauthorized, message: "Unauthorized")
        }
        return user
    }
    
}
<commit_msg>Boop - copy create to update<commit_after>//
//  PodcastController.swift
//  DebuggedPodcast
//
//  Created by <NAME> on 4/8/17.
//
//

import Vapor
import HTTP
import Foundation
import FormData

class PodcastController: ResourceRepresentable {
    func index(request: Request) throws -> ResponseRepresentable {
        return try Podcast.all().makeNode().converted(to: JSON.self)
    }
    
    func create(request: Request) throws -> ResponseRepresentable {
        let user = try requireAuth(req: request)
        let metadata = try Metadata.all().first
        
        let params = try Validator.validate(req: request, expected:
            [
                K.API.Title : Type.String,
                K.API.Subtitle : Type.String,
                K.API.Author : Type.String,
                K.API.Summary : Type.String,
                K.API.ImageURL : Type.String,
                K.API.MediaDuration : Type.String
            ])
        
        guard let file = request.data[K.API.Media] as? FormData.Field else {
            throw Abort.custom(status: Status.badRequest, message: "media should be type: Multipart Binary")
        }
        
        guard let filename = file.filename else {
            throw Abort.badRequest
        }
        
        try Data(file.part.body).write(to: URL(fileURLWithPath: drop.workDir + "/Public/media/\(filename)"))
        
        let df:DateFormatter = DateFormatter()
        df.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format
        df.locale = Locale(identifier: "en_US_POSIX")
        let date:String = df.string(from: Date())
        
        var podcast = Podcast(title: params.string[K.API.Title] ?? "",
                              author: params.string[K.API.Author] ?? "",
                              subtitle: params.string[K.API.Subtitle] ?? "",
                              summary: params.string[K.API.Summary] ?? "",
                              imageURL: params.string[K.API.ImageURL] ?? "",
                              mediaURL: "./media/\(filename)",
                              mediaLength: file.part.body.count,
                              mediaType: "audio/mpeg",
                              mediaDuration: params.string[K.API.MediaDuration] ?? "",
                              GUID: UUID().uuidString,
                              publishDate: date,
                              createdBy: user.id?.int ?? -1,
                              metadata: metadata?.id?.int ?? -1)
        
        try podcast.save()
        return podcast
    }
    
    func show(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        return podcast
    }
    
    func delete(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        _ = try requireAuth(req: request)
        
        try podcast.delete()
        return JSON([:])
    }
    
    func clear(request: Request) throws -> ResponseRepresentable {
        _ = try requireAuth(req: request)
        
        try Podcast.query().delete()
        return JSON([])
    }
    
    func update(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        let user = try requireAuth(req: request)
        let metadata = try Metadata.all().first
        
        let params = try Validator.validate(req: request, expected:
            [
                K.API.Title : Type.String,
                K.API.Subtitle : Type.String,
                K.API.Author : Type.String,
                K.API.Summary : Type.String,
                K.API.ImageURL : Type.String,
                K.API.MediaDuration : Type.String
            ])
        
        guard let file = request.data[K.API.Media] as? FormData.Field else {
            throw Abort.custom(status: Status.badRequest, message: "media should be type: Multipart Binary")
        }
        
        guard let filename = file.filename else {
            throw Abort.badRequest
        }
        
        try Data(file.part.body).write(to: URL(fileURLWithPath: drop.workDir + "/Public/media/\(filename)"))
        
        let df:DateFormatter = DateFormatter()
        df.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format
        df.locale = Locale(identifier: "en_US_POSIX")
        let date:String = df.string(from: Date())
        
        var podcast = Podcast(title: params.string[K.API.Title] ?? "",
                              author: params.string[K.API.Author] ?? "",
                              subtitle: params.string[K.API.Subtitle] ?? "",
                              summary: params.string[K.API.Summary] ?? "",
                              imageURL: params.string[K.API.ImageURL] ?? "",
                              mediaURL: "./media/\(filename)",
            mediaLength: file.part.body.count,
            mediaType: "audio/mpeg",
            mediaDuration: params.string[K.API.MediaDuration] ?? "",
            GUID: UUID().uuidString,
            publishDate: date,
            createdBy: user.id?.int ?? -1,
            metadata: metadata?.id?.int ?? -1)
        
        try podcast.save()
        return podcast
    }
    
    func replace(request: Request, podcast: Podcast) throws -> ResponseRepresentable {
        _ = try requireAuth(req: request)
        try podcast.delete()
        return try create(request: request)
    }
    
    func makeResource() -> Resource<Podcast> {
        return Resource(
            index: index,
            store: create,
            show: show,
            replace: replace,
            modify: update,
            destroy: delete,
            clear: clear
        )
    }
    
    private func requireAuth(req: Request) throws -> User {
        guard let user = try req.auth.user() as? User else {
            throw Abort.custom(status: Status.unauthorized, message: "Unauthorized")
        }
        return user
    }
    
}

{'fraction_non_alphanumeric': 0.111681308167348, 'fraction_numerical': 0.0025223971470818476, 'mean_word_length': 1.497936128611775, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3392832', 'n_tokens_mistral': 3075, 'n_tokens_neox': 3009, 'n_words': 662}