예제 #1
0
        public Model.Litters Litter(int id, long userid)
        {
            var litter = context.Litters.Find(id);

            if (litter == null)
            {
                // Use local time instead of server time to ensure we have the right day
                var timezone = System.TimeZoneInfo.CreateCustomTimeZone("NZST", new System.TimeSpan(12, 00, 00), "NZST", "NZST");
                var today    = System.TimeZoneInfo.ConvertTimeToUtc(System.DateTime.Now);
                today = System.TimeZoneInfo.ConvertTimeFromUtc(today, timezone).Date;

                litter = new Model.Litters
                {
                    UserId      = userid,
                    Animal      = "cat",
                    WeeksToWean = 0,
                    BornOn      = today,
                    Listed      = today
                };
            }
            litter.User = context.Users.Find(litter.UserId);
            if (litter.User != null)
            {
                litter.User.Litters     = null;
                litter.User.Token       = null;
                litter.User.BankAccount = null;
            }
            litter.Animals = context.Animals.Where(a => a.LitterId == id).ToList();
            foreach (Model.Animals a in litter.Animals)
            {
                a.Litter = null;
            }
            return(litter);
        }
예제 #2
0
        public int SaveLitter()
        {
            string json = new StreamReader(Request.Body).ReadToEnd();

            Model.Litters litter = JsonConvert.DeserializeObject <Model.Litters>(json);

            var record = context.Litters.Find(litter.Id);

            if (record == null)
            {
                record = new Model.Litters();
                context.Litters.Add(record);
                record.UserId = litter.UserId;
                record.Listed = litter.Listed;
            }
            else
            {
                if (litter.PictureUrl != record.PictureUrl)
                {
                    DeleteImage(record.PictureUrl);
                }
            }

            var token = this.Request.Headers["Authorization"][0].Replace("Bearer ", "");

            Model.Users bearer = context.Users.FirstOrDefault(u => u.Token == token);
            if (bearer == null || bearer.Id != record.UserId)
            {
                return(0);                                              // security check
            }
            record.BornOn       = litter.BornOn;
            record.WeeksToWean  = litter.WeeksToWean;
            record.Price        = litter.Price;
            record.Deposit      = litter.Deposit;
            record.Animal       = litter.Animal;
            record.Breed        = litter.Breed;
            record.PictureUrl   = litter.PictureUrl;
            record.Description  = litter.Description;
            record.IsIndividual = litter.IsIndividual;
            context.SaveChanges();

            foreach (Model.Animals animal in litter.Animals)
            {
                animal.LitterId = record.Id;
                SaveAnimalToDb(animal);
            }

            return(record.Id);
        }