public CommunityApiModel MapGet(Community community, LocationElemensDto locationElementsDto, List <Section> sections)
        {
            BasicInfoApiModel        basicInfoApiModel         = new BasicInfoApiModel();
            AssociationApiModel      associationApiModel       = new AssociationApiModel();
            LocationApiModel         locationApiModel          = new LocationApiModel();
            SelectedLocationApiModel selectedLocationViewModel = new SelectedLocationApiModel();

            _mapper.Map(community, basicInfoApiModel);
            _mapper.Map(community.Association, associationApiModel);
            _mapper.Map(community.Location, locationApiModel);

            selectedLocationViewModel.Countries = Map(locationElementsDto.Countries);
            selectedLocationViewModel.States    = Map(locationElementsDto.States);
            selectedLocationViewModel.Cities    = Map(locationElementsDto.Cities);
            selectedLocationViewModel.Areas     = Map(locationElementsDto.Areas);
            selectedLocationViewModel.SubAreas  = Map(locationElementsDto.SubAreas);

            JObject jObject = new JObject();

            Map(jObject, sections);
            return(new CommunityApiModel()
            {
                BasicInfo = basicInfoApiModel,
                Association = associationApiModel,
                SelectedLocation = selectedLocationViewModel,
                Sections = jObject.ToObject <Dictionary <string, object> >()
            });
        }
        public HttpResponseMessage Put(int id, LocationApiModel locationApiModel)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponseForInvalidModelState());
            }

            using (var repos = DataRepos.Instance)
            {
                if (!id.IsLocationOwner(RequestContext.GetUserId(), repos.Locations))
                {
                    return(Request.CreateResponseForNotOwner());
                }

                Location location = null;
                try
                {
                    location = locationApiModel.CreateLocationFromModel(RequestContext.GetUserId(), repos.Locations, User.Identity);
                }
                catch (ArgumentException aEx)
                {
                    return(Request.CreateResponseForUnprocessableEntity(aEx.Message != null ? aEx.Message : null));
                }

                if (location.Name.Length < 1)
                {
                    return(Request.CreateResponseForUnprocessableEntity("Location must have a valid name"));
                }
                else if (location.Name.Length > _MAX_LOCATION_NAME_LENGTH)
                {
                    location.Name = location.Name.Substring(0, _MAX_LOCATION_NAME_LENGTH);
                }

                if (location.Desc.Length > _MAX_LOCATION_DESCRIPTION_LENGTH)
                {
                    location.Desc = location.Desc.Substring(0, _MAX_LOCATION_DESCRIPTION_LENGTH);
                }

                var result = repos.Locations.Update(id, location, false);

                if (result)
                {
                    //event
                    EventService.Instance.Enqueue(new LocationUpdated()
                    {
                        Location = locationApiModel.BuildDTO(id, location.Type),
                    });

                    return(Request.CreateResponse(HttpStatusCode.NoContent));
                }

                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
        }
Exemplo n.º 3
0
        public static Location CreateLocationFromModel(this LocationApiModel model, string userId, ILocationsRepository repository, IIdentity identity)
        {
            int?sCatId = null;
            var pCat   = repository.GetCategoryByName(model.PCat);

            if (pCat == null)
            {
                throw new ArgumentException("primary category not found");
            }

            if (model.SCat != null)
            {
                var sCat = repository.GetCategoryByName(model.SCat);
                if (sCat == null)
                {
                    sCatId = repository.InsertCategory(model.SCat, pCat.Id);
                }
                else if (!sCat.Aggregate.HasValue || sCat.Aggregate.Value != pCat.Id)
                {
                    var message = string.Format("categories aren't consistent, {0} is a ", sCat.Name);
                    if (sCat.Aggregate.HasValue)
                    {
                        message += string.Format("sub-category of {0}",
                                                 repository.GetCategoryById(sCat.Aggregate.Value).Name);
                    }
                    else
                    {
                        message += "primary category";
                    }

                    throw new ArgumentException(message);
                }
                else
                {
                    sCatId = sCat.Id;
                }
            }

            var typeId = identity.IsAdmin() ? 1 : 0;

            return(new Location
            {
                UserId = userId,
                PCatId = pCat.Id,
                SCatId = sCatId,
                Name = model.Name,
                Lat = model.Lat,
                Lng = model.Lng,
                Desc = model.Desc,
                Type = Types.Values[typeId]
            });
        }
 public static LocationDTO BuildDTO(this LocationApiModel locApi, int id, string type)
 {
     return(new LocationDTO
     {
         Id = id,
         PCat = locApi.PCat,
         SCat = locApi.SCat,
         Name = locApi.Name,
         Lat = locApi.Lat,
         Lng = locApi.Lng,
         Desc = locApi.Desc,
         Type = type
     });
 }
        public HttpResponseMessage Post(LocationApiModel locationApiModel)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponseForInvalidModelState());
            }

            using (var repos = DataRepos.Instance)
            {
                Location location = null;
                try
                {
                    location = locationApiModel.CreateLocationFromModel(RequestContext.GetUserId(), repos.Locations, User.Identity);
                }
                catch (ArgumentException aEx)
                {
                    return(Request.CreateResponseForUnprocessableEntity(aEx.Message != null ? aEx.Message : null));
                }

                if (location.Name.Length < 1)
                {
                    return(Request.CreateResponseForUnprocessableEntity("Location must have a valid name"));
                }
                else if (location.Name.Length > _MAX_LOCATION_NAME_LENGTH)
                {
                    location.Name = location.Name.Substring(0, _MAX_LOCATION_NAME_LENGTH);
                }

                if (location.Desc.Length > _MAX_LOCATION_DESCRIPTION_LENGTH)
                {
                    location.Desc = location.Desc.Substring(0, _MAX_LOCATION_DESCRIPTION_LENGTH);
                }

                int id = repos.Locations.Insert(location);

                //event
                EventService.Instance.Enqueue(new LocationCreated()
                {
                    Location = locationApiModel.BuildDTO(id, location.Type),
                });

                return(id != -1 ? Request.CreateResponse(HttpStatusCode.OK, id) :
                       Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
Exemplo n.º 6
0
        public async Task <IHttpActionResult> Location(LocationApiModel model)
        {
            if (ModelState.IsValid && model != null)
            {
                collection = mongoDb.GetCollection <object>("location");

                var userId = (await db.Devices.FirstAsync(x => x.UniqueId.Equals(model.DeviceId))).UserId;

                var mapped = Mapper.Map <LocationModel>(model);
                mapped.Created = DateTime.Now;
                mapped.UserId  = userId;

                var bson = BsonSerializer.Deserialize <ExpandoObject>(BsonDocument.Parse(JsonConvert.SerializeObject(mapped)));

                await collection.InsertOneAsync(bson);

                NotificationHub.GetHubContext().Clients.All.location(mapped);

                return(Ok(new ApiResponse(200, mapped)));
            }

            return(this.BadRequest(new ApiResponse(400)));
        }