Esempio n. 1
0
        public async Task <IHttpActionResult> EditStationAsync(StationDTO station)
        {
            /* Check if model is valid */
            if (!ModelState.IsValid)
            {
                var errors = ModelState.ToDictionary(
                    kvp => kvp.Key,
                    kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
                    );
                return(Content(HttpStatusCode.BadRequest, errors));
            }
            string token  = Request.Headers.GetValues("Authorization").FirstOrDefault();
            int    userId = JwtHelper.Instance.GetUserId(token);

            IStationDao stationDao    = AdoFactory.Instance.GetStationDao("wetr");
            Station     stationToEdit = await stationDao.FindByIdAsync(station.StationId);

            /* If the station doesn't belong to the user */
            if (stationToEdit.UserId != userId)
            {
                return(Content(HttpStatusCode.Forbidden, new object()));
            }

            /* Create new address */
            IAddressDao addressDao = AdoFactory.Instance.GetAddressDao("wetr");

            await addressDao.UpdateAsync(new Address()
            {
                AddressId   = stationToEdit.AddressId,
                CommunityId = station.CommunityId,
                Location    = station.Location,
                Zip         = "NO_ZIP"
            });

            int newAddressId = Convert.ToInt32((await addressDao.GetNextId()) - 1);

            station.AddressId = newAddressId;


            /* Edit Station */
            await stationDao.UpdateAsync(station.ToStation());

            return(Content(HttpStatusCode.OK, new object()));
        }
Esempio n. 2
0
        public async Task <long> AddNewAddress(Address address)
        {
            if (!CheckAddress(address))
            {
                return(-1);
            }
            if (!await addressDao.InsertAsync(address))
            {
                return(-1);
            }

            try
            {
                return(await addressDao.GetNextId() - 1);
            }
            catch (Common.Dal.Ado.MySqlException ex)
            {
                throw new BusinessSqlException(ex.Message, ex.InnerException);
            }
        }
Esempio n. 3
0
        public async Task <IHttpActionResult> CreateStation(StationDTO station)
        {
            /* Check if model is valid */
            if (!ModelState.IsValid)
            {
                var errors = ModelState.ToDictionary(
                    kvp => kvp.Key,
                    kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
                    );
                return(Content(HttpStatusCode.BadRequest, errors));
            }

            string token  = Request.Headers.GetValues("Authorization").FirstOrDefault();
            int    userId = JwtHelper.Instance.GetUserId(token);

            IStationDao stationDao = AdoFactory.Instance.GetStationDao("wetr");
            IAddressDao addressDao = AdoFactory.Instance.GetAddressDao("wetr");



            await addressDao.InsertAsync(new Address()
            {
                CommunityId = station.CommunityId,
                Location    = station.Location,
                Zip         = "NO_ZIP"
            });

            int newAddressId = Convert.ToInt32((await addressDao.GetNextId()) - 1);

            /* Cleanup */
            station.AddressId = newAddressId;
            station.StationId = 0;
            station.UserId    = userId;

            await stationDao.InsertAsync(station.ToStation());

            return(Content(HttpStatusCode.OK, new object()));
        }