コード例 #1
0
        //audio/*
        #endregion

        #region Methods

        /// <summary>
        /// Saves the current RecordModel as a AdviceJsonItem.
        /// </summary>
        /// <returns>Returns an AdviceJsonItem of the current RecordModel.</returns>
        public AdviceJsonItem SaveTaxa()
        {
            string baseString;
            List <AdviceImageJsonItem> baseList  = new List <AdviceImageJsonItem>();
            List <AdviceImageJsonItem> audioList = new List <AdviceImageJsonItem>();
            var fileHelper = DependencyService.Get <IFileHelper>();

            foreach (var media in SelectedMedia)
            {
                baseString = fileHelper.GetBase64FromImagePath(media.Path);
                baseList.Add(new AdviceImageJsonItem(baseString, media.Path));
            }
            var            tempTaxon      = ((App)App.Current).Taxa.FirstOrDefault(i => i.TaxonId == (int)(_selectedRecord.TaxonId));
            var            taxonName      = (tempTaxon != null) ? tempTaxon.TaxonName : "";
            AdviceJsonItem adviceJsonItem = new AdviceJsonItem
            {
                AdviceId       = _selectedRecord.LocalRecordId,
                Identifier     = _selectedRecord.Identifier,
                TaxonId        = _selectedRecord.TaxonId,
                TaxonFullName  = taxonName,
                AdviceDate     = _selectedRecord.RecordDate,
                AdviceCount    = _selectedRecord.TotalCount,
                AdviceCity     = _selectedRecord.HabitatName,
                MaleCount      = _selectedRecord.MaleCount,
                FemaleCount    = _selectedRecord.FemaleCount,
                StateEgg       = false,
                StateLarva     = false,
                StateImago     = true,
                StateNymph     = false,
                StatePupa      = false,
                StateDead      = false,
                Comment        = _selectedRecord.HabitatDescription,
                ReportedByName = _selectedRecord.ReportedByName,
                ImageCopyright = "",
                ImageLegend    = "",
                UploadCode     = 0,
                Lat            = _selectedRecord.Position == PositionOption.Pin ? (decimal)_selectedRecord.Latitude : (decimal)RecordEdit.GetCenterOfPositions(PositionList).Latitude,
                Lon            = _selectedRecord.Position == PositionOption.Pin ? (decimal)_selectedRecord.Longitude : (decimal)RecordEdit.GetCenterOfPositions(PositionList).Longitude,
                //AreaWkt = _selectedRecord.Position != PositionOption.Pin ? ConvertPositionListToWkt(_selectedRecord.Position, PositionList) : "",
                Zoom               = _selectedRecord.Height != null ? (int)_selectedRecord.Height : 1,
                AccuracyType       = 1,
                DeviceId           = DependencyService.Get <IDeviceId>().GetDeviceId(),
                DeviceHash         = Database.GetRegister().Result.DeviceHash,
                LocalityTemplateId = 0,
                Images             = baseList.ToArray()
            };

            return(adviceJsonItem);
        }
コード例 #2
0
        //POST : /api/Advice/SaveAdvice/Mobile
        public async Task <ActionResult> PostSaveAdviceFromMobile(AdviceJsonItem adviceObject)
        {
            try
            {
                //_userId = User.Claims.First(i => i.Type == "UserId").Value;
                ApplicationUser _user = _userManager.Users.First(u => u.Email == adviceObject.UserName);
                if (_user != null)
                {
                    _userId = _user.Id;
                    var obsId          = 0;
                    int?taxonId_edited = null;
                    //if user users horn-old app version --> name lookup
                    if (_infContext.Taxon.FirstOrDefault(tx => tx.TaxonId == adviceObject.TaxonId) == null)
                    {
                        taxonId_edited = _infContext.Taxon.Where(tx => tx.TaxonName == adviceObject.TaxonFullName).Select(tx => tx.TaxonId).FirstOrDefault();
                    }

                    if (adviceObject.ObservationId == 0)
                    {
                        Event adviceEvent = new Event();
                        adviceEvent.AccuracyId   = adviceObject.AccuracyType;
                        adviceEvent.LocalityName = adviceObject.AdviceCity;
                        //adviceEvent. = adviceObject.AdviceCount;
                        //adviceEvent.TkNr = adviceObject.AreaWkt;
                        adviceEvent.LatitudeDecimal  = adviceObject.Lat;
                        adviceEvent.LongitudeDecimal = adviceObject.Lon;
                        adviceEvent.UserId           = _userId;
                        adviceEvent.AuthorName       = adviceObject.ReportedByName;
                        adviceEvent.TkNr             = await GetTk25Id(adviceEvent.LatitudeDecimal, adviceEvent.LongitudeDecimal);

                        _obsContext.Event.Add(adviceEvent);
                        _obsContext.SaveChanges();

                        Observation adviceObservation = new Observation();
                        adviceObservation.EventId = adviceEvent.EventId;
                        adviceObservation.UserId  = _userId;

                        if (adviceObservation.ApprovalStateId == null)
                        {
                            adviceObservation.ApprovalStateId = 1;
                        }
                        adviceObservation.AdviceCount = adviceObject.AdviceCount;
                        adviceObservation.AuthorName  = adviceObject.ReportedByName;
                        adviceObservation.UserId      = _userId;
                        //adviceObservation.DiagnosisTypeId = adviceObject.Diagn;
                        adviceObservation.FemaleCount = adviceObject.FemaleCount;
                        adviceObservation.HabitatDate = adviceObject.AdviceDate;
                        //adviceObservation.HabitatDateTo = adviceObject.AdviceCount;
                        //adviceObservation.JuvenileCount = adviceObject.Cou;
                        adviceObservation.LocalityTypeId     = adviceObject.LocalityTemplateId;
                        adviceObservation.MaleCount          = adviceObject.MaleCount;
                        adviceObservation.ObservationComment = adviceObject.Comment;
                        //adviceObservation.SizeGroupId = adviceObject.AdviceCount;

                        if (taxonId_edited != null)
                        {
                            adviceObservation.TaxonId = (int)taxonId_edited;
                        }
                        else
                        {
                            adviceObservation.TaxonId = adviceObject.TaxonId;
                        }
                        adviceObservation.TaxonName    = adviceObject.TaxonFullName;
                        adviceObservation.LastEditDate = DateTime.Now;
                        _obsContext.Observation.Add(adviceObservation);
                        _obsContext.SaveChanges();

                        obsId = adviceObservation.ObservationId;

                        foreach (AdviceImageJsonItem img_in in adviceObject.Images)
                        {
                            byte[] imageBytes = Convert.FromBase64String(img_in.ImageBase64);
                            string filename   = Path.Combine(imgSavePath, img_in.ImageName);
                            System.IO.File.WriteAllBytes(filename, imageBytes);

                            //Method to save images @bodentierhochvier.de / wordpress
                            //--> imgName
                            Image img_new = new Image();
                            img_new.ImagePath = "user_uploads/" + img_in.ImageName;
                            //img_new.CmsId = img.CmsId;
                            img_new.ObservationId = obsId;
                            img_new.Description   = adviceObservation.ObservationComment;
                            //img_new.CopyrightText = img.CopyrightText;
                            img_new.LicenseId = 1;
                            img_new.UserId    = _userId;
                            img_new.Author    = adviceEvent.AuthorName;
                            if (taxonId_edited != null)
                            {
                                img_new.TaxonId = (int)taxonId_edited;
                            }
                            else
                            {
                                img_new.TaxonId = adviceObservation.TaxonId;
                            }
                            img_new.TaxonName = adviceObservation.TaxonName;
                            _obsContext.Add(img_new);
                        }
                    }
                    else
                    {
                        var obs_tbu = _obsContext.Observation.FirstOrDefault(o => o.ObservationId == adviceObject.ObservationId);
                        obs_tbu.UserId      = _userId;
                        obs_tbu.AdviceCount = adviceObject.AdviceCount;
                        obs_tbu.AuthorName  = adviceObject.Name;
                        //adviceObservation.DiagnosisTypeId = adviceObject.Diagn;
                        obs_tbu.FemaleCount = adviceObject.FemaleCount;
                        obs_tbu.HabitatDate = adviceObject.AdviceDate;
                        //adviceObservation.HabitatDateTo = adviceObject.AdviceCount;
                        //adviceObservation.JuvenileCount = adviceObject.Cou;
                        obs_tbu.LocalityTypeId     = adviceObject.LocalityTemplateId;
                        obs_tbu.MaleCount          = adviceObject.MaleCount;
                        obs_tbu.ObservationComment = adviceObject.Comment;
                        //adviceObservation.SizeGroupId = adviceObject.AdviceCount;

                        if (taxonId_edited != null)
                        {
                            obs_tbu.TaxonId = (int)taxonId_edited;
                        }
                        else
                        {
                            obs_tbu.TaxonId = adviceObject.TaxonId;
                        }
                        obs_tbu.TaxonName    = adviceObject.TaxonFullName;
                        obs_tbu.LastEditDate = DateTime.Now;
                        _obsContext.Observation.Update(obs_tbu);
                        _obsContext.SaveChanges();
                        obsId = obs_tbu.ObservationId;
                    }

                    _obsContext.SaveChanges();

                    return(Ok(new { succeeded = true, errors = new string[] { }, ObservationId = obsId }));
                }
                else
                {
                    return(Unauthorized());
                }
            }
            catch (Exception e)
            {
                return(BadRequest(JsonConvert.SerializeObject(e.Message)));
            }
        }