Exemplo n.º 1
0
        public async Task <ActionResult> AddPhotos(int id, List <IFormFile> files, string getMethodName)
        {
            int addedPhotoCount = 0;

            if (MultiPhoto())
            {
                var baseQuery = _photoRepo.GetDataRecordByTableName <IDataItemWithMultiplePhotos>(GetTableName());
                if (baseQuery == null)
                {
                    return(BadRequest("Could not get the record " + GetTableName() + " : " + id));
                }
                var recordFromRepo = await baseQuery.Include(r => r.Photos).FirstOrDefaultAsync(r => r.Id == id);

                if (recordFromRepo == null)
                {
                    return(NotFound());
                }
                var recordWithAppUser = recordFromRepo as IAppUserLinkable;
                if (recordWithAppUser != null && !await this.MatchAppUserWithToken((int)recordWithAppUser.AppUserId))
                {
                    return(Unauthorized());
                }
                addedPhotoCount = await _photoRepo.AddPhotosToRecord(recordFromRepo, files);
            }
            else
            {
                var baseQuery = _photoRepo.GetDataRecordByTableName <IDataItemWithSinglePhoto>(GetTableName());
                if (baseQuery == null)
                {
                    return(BadRequest("Could not get the record " + GetTableName() + " : " + id));
                }
                var recordFromRepo = await baseQuery.Include(r => r.Photo).FirstOrDefaultAsync(r => r.Id == id);

                if (recordFromRepo == null)
                {
                    return(NotFound());
                }
                var recordWithAppUser = recordFromRepo as IAppUserLinkable;
                if (recordWithAppUser != null && !await this.MatchAppUserWithToken((int)recordWithAppUser.AppUserId))
                {
                    return(Unauthorized());
                }
                addedPhotoCount = await _photoRepo.AddPhotoToRecord(recordFromRepo, files);
            }


            if (addedPhotoCount == 0)
            {
                return(BadRequest("写真を追加できませんでした"));
            }

            return(CreatedAtRoute(getMethodName, new { id = id }, null));
        }